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