开发者

Find::File preprocess

I'm trying to specify a directory, and recursively find every file in the sub-directories. After find chdir's into a di开发者_如何学运维rectory, I want to do some processing before find reads the files. Here is a simplified snippet that demonstrates the problem. It doesn't recurse into the subdirectories, but it looks like it should. I can verify that the sub-dirs and files exist because if I call find without the preprocess key then I get the listing. I haven't been using Perl for that long so I'm kind of stumped.


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";
}

And here is the output.


Calling wanted...
        /home/nelson/invoices
Calling preprocess...
        /home/nelson/invoices
Calling wanted...
        /home/nelson/invoices/1


The preprocess function is expected to return a (possibly modified) list of items to examine further. In your example, you can add @_; at the end of preprocess to return the arguments unmodified. You can do something like grep { $_ !~ /pattern/ } @_ to filter out unwanted items, and so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜