The first argument of find() is a reference to a subroutine. This is where you tell find what you want done with the filenames it finds. Find will pass the names it finds into $_ . Say you wanted to print a list of all the files below your current directory:
find sub{print $_,"\n"}, "./"
The second parameter to find() takes an array of directories. If you only have one directory, as above, that is fine. Remember that perl passes parameters in a flat list so a single scalar will pass the same as an array of one element.
You can try this from the command line to get familiar with the concept:
%perl -MFile::Find -e 'find sub{print $_,"\n"}, "./"'
Of course find() really shines when iterating over a list of directories: find sub ( do-something }, @directories Note: This can return a lot of files as find() works recursively down through all the sub directories.
Find does not return the found files so this: @array = find(); Doesn't work. You have to store the files passed to you yourself if you want to keep them. Like this: find sub { push @filelist, $_ }, @directories;
Of course if you subroutine is more complicated then it can be written separately and a reference to it passed to find().
sub my_Sub{ do-something-with-$_ . . } find \&my_Sub, @directories;
In reply to Re: how to use file::Find ?
by starbolin
in thread how to use file::Find ?
by steph_bow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |