http://qs321.pair.com?node_id=438721


in reply to help needed with File::Find and arrays.

Here is my find example. I use this when I need to remember how to use find. Course I didn't know about File::Find::Rule! Good to know.

use strict; use File::Find; print "starting a find for all files:\n"; my $files = myfind('.'); printf "Found %d files.\n", scalar(@{$files}); print "starting a find for all files matching critera:\n"; $files = myfind('.', \&criteria); printf "Found %d files.\n", scalar(@{$files}); # critera function is called by find for each file. # passed the filename found, and must return boolean # indicating whether to include the file in the result # or not. sub criteria { my $filename = shift; return $filename =~ /example/i; } # find function that will return an array or array reference # containing the files that match the criteria. # accepts two parameters: # directory to search # optional criteria function. # sub myfind { my $dir = shift; my $criteriaFunc = shift || sub{1;}; my $list = []; find({wanted=>sub{findcb($list, $criteriaFunc)}}, $dir); return((wantarray)?(@$list):($list)); } # callback function called by find each time a file is found. # this callback will add the file to the listref if it matches # the critera. sub findcb { my $listref = shift; my $criteriaFunc = shift; if($criteriaFunc->($File::Find::name)) { push @$listref, $File::Find::name; } }

"Look, Shiny Things!" is not a better business strategy than compatibility and reuse.