Nelson
Nelson

Reputation: 1747

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 directory, 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

Upvotes: 1

Views: 2441

Answers (1)

Andy
Andy

Reputation: 4866

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.

Upvotes: 5

Related Questions