Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Thursday September 30, 2004
01:38 PM


[ #21125 ]

For a long time I've wanted to create a library of ready-to-use functions to pass to File::Find::find(). I can never remember which File::Find variable is what, and I never have liked the idea that I had to code the accculumation of the filenames if I did not process them immediately. This sorrt of thing seemed like it could use a shot of code reuse mojo.

So I created File::Find::Closures to hold all of those sorts of callbacks. The functions in File::Find::Closures have names like find_by_regex(), and return two things: the callback which I give directly to find(), and a reporter closure that can access the list of files the callback acccumulated.

I thought Andy was going in this direction with File::Find::Wanted, but I think he had something else in mind. Randal came close to what I wanted with File::Finder, but without the re-use.

Maybe this is just one of those things that everyone ends up writing for himself.

So far, Find::Find::Closures is a developer release, so you won't be able to get it through It has no prereqs (other than File::Find which it uses in the tests). I've outlined what other people need to do to write their own functions for the module, and hope that people will send me cool ones that I can include.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • All I intended for FFW was to be able to say
    my @files = find_wanted( sub { return ! -d $_ }, $startdir );
    which would save the calling program from having to build up the list. That's all.

    Now, it IS entirely possible that you could have helper functions, like:

    sub empty { return -s $_ == 0 }
    sub is_dir { return -d $_ }

    my @dirs = find_wanted( \&is_dir, $start );


  • The idea is sound, but I'd rather prefer a different interface.

    At the very least I'd like to be able to inline the generator call in the find() call, maybe by passing in a reference to where to store the iterator. Actually, that makes it really simple to handle the common case where one simply wants a list of files just pass an array reference.

    my $iter;
    File::Find::find( find_by_name( \$iter => qw( README ) ), @directories );
    while( my $file = $iter->() ) {
        # ...

    # or

    my @list;