por*_*ton 1 perl directory-structure perl5
获取给定目录中所有文件(包括子目录中的文件)全名的最简单方法是什么?
是的,我了解File::Find模块。但是,有没有更简单的方法?
该文件::查找::规则是一个非常有用的工具
perl -Mstrict -MFile::Find::Rule -wE'
my @files = File::Find::Rule->file->in(".");
say for @files'
Run Code Online (Sandbox Code Playgroud)
您可以先获取对象my $ffr = File::Find::Rule,然后在其上设置规则。该规则->file仅使它不返回目录,而仍然递归。有许多此类“规则”可以对行为进行微调。我确实发现它的性能在某些情况下会变慢。请参阅最后的链接。
核心File :: Find可以完成上面的包装工作,也许还需要做更多的工作
perl -Mstrict -MFile::Find -wE'
my @dirs = (@ARGV ? @ARGV : ".");
my @files;
find( sub { push @files, $File::Find::name if -f }, @dirs );
say for @files;
' dir1 dir2 ... (or pass nothing, to scan ".")
Run Code Online (Sandbox Code Playgroud)
通过-ffiletest仅收集常规文件。我添加了一个如何(可选)传递目录列表以单线扫描的示例。据我所知,它仍然是性能最好的模块。在某些用途中,它可能会更快。
该路径:: Tiny->迭代器是非常好的,以及,对于“懒洋洋地”走一棵树
perl -Mstrict -MPath::Tiny -wE'
my $dir = shift // ".";
my $iter = path($dir)->iterator({recurse => 1});
while (my $path = $iter->()) { say $path }'
Run Code Online (Sandbox Code Playgroud)
您可以通过多种方式方便地询问遇到的内容。该模块还有许多其他工具,可用于文件系统工作,这几乎是一个“侧面”功能。
同样来自作者的Path :: Iterator :: RulePath::Tiny也提供了惰性迭代,但它是一个完整的专用迭代器,其接口类似于File::Find::Rule
perl -Mstrict -MPath::Iterator::Rule -wE'
my $dir = shift // ".";
my $rule = Path::Iterator::Rule->new->not_dir->not_empty;
my $next = $rule->iter($dir);
while (my $file = $next->()) { say $file }
' dirname
Run Code Online (Sandbox Code Playgroud)
可以使用许多便捷的方法来设置规则,其中包括逻辑,文件内容查询,自定义编写的规则等等。
有关文件查找器的选择,请参见性能比较(自2013年起)。