Stories
Slash Boxes
Comments

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

schwern (1528)

schwern
  (email not shown publicly)
http://www.pobox.com/~schwern/
AOL IM: Michael Schwern (Add Buddy, Send Message)
Yahoo! ID: schwern (Add User, Send Message)

Journal of schwern (1528)

Monday July 19, 2004
11:26 AM

glob() idiom

[ #19931 ]

The folks where I'm consulting like to use */*.pl a lot. Their code is all layed out as Foo/bar.pl. This means ls */*.pl can find all their code. The handicap is they're shy of things like find and rgrep.

Anyhow, this made me think of a neat idiom to walk directories breadth-first.

my $pattern = "*.pl";
while( my @files = glob($pattern) ) {
        print join "\n", @files;
        $pattern = "*/$pattern";
}

Beats the pants off File::Find and File::Find::Rule in terms of performance.

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.
  • It may well work faster, unless glob() is still implemented by call /bin/csh on your box.

    Of course, I don't know of a platform that still does things that way, but I'm sure that somebody does...

    -Dom

  • It'll quit too soon if someone created foo/bar/baz/and/another/thing/plugh.pl, yet there is any level between 1 and 5 that contains no .pl files in any directory at that level. It'll give a wrong answer if someone created a directory (or pipe or ...) that matches *.pl - you'll have someone to blame for your problem, though.

    This should work (modulo any bugs - I just typed it in off the top of my head...):

    @dirs = ( '.' );

    while (@dirs) {
        my $dir = shift @dirs;
        DIR = opendir $dir;