我试图指定一个目录,并递归地查找子目录中的每个文件。之后findCHDIR的到一个目录,我希望做一些处理之前find读取文件。这是一个演示问题的简化片段。它不会递归到子目录中,但看起来应该如此。我可以验证子目录和文件是否存在,因为如果我在find没有预处理键的情况下调用,那么我会得到列表。我很久没有使用 Perl 了,所以我有点难住了。
find({
wanted => \&wanted,
preprocess => \&preprocess
}, "/home/nelson/invoices/");
# function definitions
sub wanted {
print "Calling wanted...\n";
print "\t" . $File::Find::name . "\n";
}
sub preprocess{
print "Calling preprocess...\n";
print "\t" . $File::Find::dir . "\n";
}
Run Code Online (Sandbox Code Playgroud)
这是输出。
Calling wanted...
/home/nelson/invoices
Calling preprocess...
/home/nelson/invoices
Calling wanted...
/home/nelson/invoices/1
Run Code Online (Sandbox Code Playgroud)
该preprocess函数应返回一个(可能已修改的)项目列表以进行进一步检查。在您的示例中,您可以@_;在末尾添加preprocess以返回未修改的参数。您可以执行一些操作,例如grep { $_ !~ /pattern/ } @_过滤掉不需要的项目等。