in reply to file globbing in a nested while loop

G'day George,

There's a module that does exactly what you're looking for, it's called File::Find. It allows you to traverse a file tree, and (should you so desire) perform operations based upon each file you come across.

Here's some code that demonstrates how to use File::Find to find and print the location of .pl files.

use File::Find; find(\&printer, "/path/to/dir"); sub printer { print "I have found $File::Find::name\n" if /\.pl$/; }
If you don't like to specify the sub-routine seperately, you can do it with an anonymous subroutine.
use File::Find find( sub {print "I have found $File::Find::name\n" if /\.pl$/;}, "/path/to/dir");
Please note that newer versions of File::Find contain some very rich features, much more than the manual page here on PerlMonks. Check your local install to see what extra fun you can find.

Hope you find this useful.

Cheers,
Paul

Replies are listed 'Best First'.
Re: Re: file globbing in a nested while loop
by George_Sherston (Vicar) on Oct 05, 2001 at 17:02 UTC
    Thanks, and ditto to busunsl. That's certainly solved the problem - I can write my script now, and have re-learnt the ever valuable lesson that time spent researching CPAN is rarely time wasted. But I'd still be very interested if anyone can shed light on why my own attempt didn't work. Is the while loop not restarting? And if so why not?

    § George Sherston
      If this runs on Unix, it might be you are hitting the '.' directory.

      You would be looping on that till eternity.

        Normal globbing doesn't return the dot files.