我发现 File::Find::find
背后的设计选择有点意外。我遇到的所有示例都显示 find
在 void 上下文中使用。
documentation还澄清了 find(\&wanted, @dirs )
中的 \&wanted
coderef 并不是一个过滤器(强调我自己的):
The
wanted()
function does whatever verifications you want on each file and directory. Note that despite its name, thewanted()
function is a generic callback function, and does not tellFile::Find
if a file is "wanted" or not. In fact, its return value is ignored.
但是如果我确实想以类似于 grep
的方式将其用作过滤器怎么办? ?我很想知道是否还有另一种写法:
use strict;
use warnings;
use feature 'say';
use File::Find;
my $wanted = qr/^\d{2}_/; # e.g.
my @wanted;
find( sub { -f && /$wanted/ && push @wanted, $_ }, '.' );
# I wish my @wanted = find( ... ); worked
say for @wanted;
最佳答案
在 CPAN 上,我发现了几个 File::Find 的替代接口(interface),旨在简化接口(interface)。
我会尝试 File::Finder
,首先由著名的 Perl 专家 Randal Schwartz 撰写。
File::Find::Rule
是另一个。
(可以肯定地说,如果人们正在编写模块来执行此操作,那么没有简单的内置方法可以做到这一点。)
关于perl - File::Find 是否有更简洁的方法来返回所需文件的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12620423/