1
我想指定一个目录,并递归查找子目录中的每个文件。在find
chdir的目录下,我想在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";
}
这里是输出。
Calling wanted...
/home/nelson/invoices
Calling preprocess...
/home/nelson/invoices
Calling wanted...
/home/nelson/invoices/1
啊哈!我没有意识到我应该从预处理函数调用中返回一个列表。这正是我想要的,谢谢。 – Nelson 2011-04-09 03:38:23