Perl readdir作为一个单行程?

时间:2022-11-02 22:32:50

As of now, I know of two ways to open and read a directory in Perl. You can use opendir, readdir and closedir or you can simply use glob to get the contents of the directory.

截至目前,我知道有两种方法可以在Perl中打开和读取目​​录。您可以使用opendir,readdir和closedir,也可以使用glob来获取目录的内容。


Example:

Using opendir, readdir and closedir:

使用opendir,readdir和closedir:

opendir my $dh, $directory;
my @contents = readdir $dh;
closedir $dh;

Using glob:

my @contents = <$directory/*>;

I have been informed that in some situations the glob method can produce unpredictable results (for example it can act differently on different operating systems when it encounters special characters in directory names).

我被告知在某些情况下,glob方法可能会产生不可预测的结果(例如,当遇到目录名中的特殊字符时,它可能在不同的操作系统上采取不同的行为)。

The thing I love about the glob method is how "quick and dirty" it is. It's one simple line and it gets the job done, but if it doesn't work in all situations that can introduce unexpected and difficult to find bugs.

我喜欢的glob方法是它的“快速和肮脏”。这是一个简单的界限,它完成了工作,但如果它不能在所有可能引入意外和难以发现的错误的情况下工作。

I was wondering if there was a sort of "shorthand" way to use the opendir, readdir, closedir method?

我想知道是否有一种“速记”方式来使用opendir,readdir,closedir方法?

Maybe something like this fancy method for slurping a file in one line I recently discovered.

也许像我最近发现的那种在一行中啜饮文件的花哨方法。

2 个解决方案

#1


How about the following?

以下怎么样?

my @contents = get_dir_contents($dir);

You even get to decide how that handles errors, if . should be returned, if .. should be returned, if "hidden" files should be returned and whether the path is prepended to the file name since you write get_dir_contents yourself.

你甚至可以决定如何处理错误。应该返回,如果..应该返回,是否应该返回“隐藏”文件以及路径是否预先添加到文件名中,因为您自己编写了get_dir_contents。


Alternatives:

  • use File::Find::Rule qw( );
    my @contents = File::Find::Rule->maxdepth(1)->in($dir);
    
  • 使用File :: Find :: Rule qw(); 我的@contents = File :: Find :: Rule-> maxdepth(1) - > in($ dir);

  • use File::Slurp qw( read_dir );
    my @contents = read_dir($dir);
    
  • 使用File :: Slurp qw(read_dir); 我的@contents = read_dir($ dir);

  • # Unix only, but that includes cygwin and OS/X.
    my @contents = <\Q$dir\E/.* \Q$dir\E/*>;
    
  • #Unix only,但包括cygwin和OS / X. 我的@contents = <\ Q $ dir \ E /.* \ Q $ dir \ E / *>;

#2


I believe I have come up with a valid one-liner that involves opendir/readdir!

我相信我已经提出了一个涉及opendir / readdir的有效单行程!

my @contents = do { opendir my $dh, $dir or die $!; readdir $dh };

This should open and read the directory, return all of its contents and as soon as the do block ends, the $dh should be closed by Perl "automagically".

这应该打开并读取目录,返回其所有内容,一旦do块结束,$ dh应该由Perl“自动”关闭。

#1


How about the following?

以下怎么样?

my @contents = get_dir_contents($dir);

You even get to decide how that handles errors, if . should be returned, if .. should be returned, if "hidden" files should be returned and whether the path is prepended to the file name since you write get_dir_contents yourself.

你甚至可以决定如何处理错误。应该返回,如果..应该返回,是否应该返回“隐藏”文件以及路径是否预先添加到文件名中,因为您自己编写了get_dir_contents。


Alternatives:

  • use File::Find::Rule qw( );
    my @contents = File::Find::Rule->maxdepth(1)->in($dir);
    
  • 使用File :: Find :: Rule qw(); 我的@contents = File :: Find :: Rule-> maxdepth(1) - > in($ dir);

  • use File::Slurp qw( read_dir );
    my @contents = read_dir($dir);
    
  • 使用File :: Slurp qw(read_dir); 我的@contents = read_dir($ dir);

  • # Unix only, but that includes cygwin and OS/X.
    my @contents = <\Q$dir\E/.* \Q$dir\E/*>;
    
  • #Unix only,但包括cygwin和OS / X. 我的@contents = <\ Q $ dir \ E /.* \ Q $ dir \ E / *>;

#2


I believe I have come up with a valid one-liner that involves opendir/readdir!

我相信我已经提出了一个涉及opendir / readdir的有效单行程!

my @contents = do { opendir my $dh, $dir or die $!; readdir $dh };

This should open and read the directory, return all of its contents and as soon as the do block ends, the $dh should be closed by Perl "automagically".

这应该打开并读取目录,返回其所有内容,一旦do块结束,$ dh应该由Perl“自动”关闭。