File::Find::Wanted released

petdance on 2004-08-06T05:13:03

I just released File::Find::Wanted 0.01.

File::Find is a great module, except that it doesn't actually find anything. Its find() function walks a directory tree and calls a callback function. Unfortunately, the callback function is deceptively called wanted, which implies that it should return a boolean saying whether you want the file. That's not how it works.

Most of the time you call find(), you just want to build a list of files. There are other modules that do this for you, most notably Richard Clamp's great File::Find::Rule, but in many cases, it's overkill, and you need to learn a new syntax.

With the find_wanted function, you supply a callback sub and a list of starting directories, but the sub actually should return a boolean saying whether you want the file in your list or not.

To get a list of all files ending in .jpg:

my @files = find_wanted( sub { -f && /\.jpg$/ }, $dir );

It's easy, direct, and simple.

The cynical may say "that's just the same as doing this":

my @files;
find( sub { push @files, $File::Find::name if -f && /\.jpg$/ }, $dir );

Sure it is, but File::Find::Wanted makes it more obvious, and saves a line of code. That's worth it to me. I'd like it if find_wanted()made its way into the File::Find distro, but for now, this will do.


Nice.

ambs on 2004-08-06T07:40:41

Fight for its inclusion in File::Find.

Nothing new... File::Finder close to this

merlyn on 2004-08-06T16:20:06

Yours:
my @files = find_wanted( sub { -f && /\.jpg$/ }, $dir );
Mine (File::Finder, in the CPAN):
my @files = File::Finder->eval(sub { -f && /\.jpg/ })->in($dir);
# or
my @files = File::Finder->type('f')->name(qr/\.jpg/)->in($dir);
But I can also do this:
my $rule = File::Finder->type('f')->name(qr/\.jpg/);
...
my @jpegs_in_here = $rule->in($here);
my @jpegs_in_there = $rule->in($there);
my @big_jpegs_over_yonder = $rule->size('+100')->in($yonder);
...

Re:Nothing new... File::Finder close to this

petdance on 2004-08-06T20:16:13

Right. I'm not looking for MORE functionality, I'm looking for SIMPLER functionality.

Re:Nothing new... File::Finder close to this

merlyn on 2004-08-07T01:12:47

Well, don't make me argue that mine are arguably simpler. {grin}

Cool

grantm on 2004-08-07T00:43:27

I think it would make an excellent addition to the standard File::Find module.