Stories
Slash Boxes
Comments
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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • While everybody's hyping up their own favourite File::Find [cpan.org] replacement, I just want to chime in and say that, for a lot of the replacement modules, I just like File::Find better. In particular, File::find::Rule [cpan.org], which I find just incomprehensible, and horrible.

    I wonder if many of the "problems" people see for File::Find are actually problems. So it uses global variables as fake parameters... but they're dynamically limited in scope to (the immediate surroundings of) the callback sub. So I don't mind ist so much. The situation is actually very much alike to the keyword this in plenty of OO languages. Besides, File::Next [cpan.org] does the same. :)

    But you do have to do it the proper way: use an anonymous sub, not a reference to a global function. That way, you end up with a closure, instead of something that depends on global variables (not File::Find's own global variables, but yours).

    Do

    use Cwd;
    sub traverse {
        my @files;
        find sub {
            push @files, $File::Find::name if /.txt$/ and -f;
        }, cwd;
        return @files;
    }

    Not

    use Cwd;
    sub wanted {
        push @files, $File::Find::name if /.txt$/ and -f;
    }
    @files = ();
    find \&wanted, cwd;

    • The point is, push @files, $File::Find::name is what everyone does. So at best it’s unnecessary noise and the module should do that automatically.

      However, the reason everyone does that is because performing complex work from within a callback is a pain. State machines are no fun to write. An iterator interface is much nicer to work with when you have something complex to do, and just as easy to use when you really just want to collect the file names. It’s much friendlier to use in programs that