in reply to How to pattern match filenames in readdir?

Several of the solutions proposed so far have the disadvantage of reading in the entire contents of the directory, before iterating over the desired filenames. This could be an issue if the directory contains a lot of files.

Here's a solution that reads in one filename at a time, as your original code does.

opendir(DIR, "$dir") || die "can't open dir $dir: $!.\n"; while (defined(my $file = readdir DIR)) { next unless /BLAH/; do something; } closedir(DIR);
The glob solution that runrig suggested, while (<*BLAH*>) {, also reads one filename at a time.

Replies are listed 'Best First'.
(tye)Re: How to pattern match filenames in readdir?
by tye (Sage) on Jun 05, 2001 at 23:05 UTC

    Actually, most implementations of Perl's glob (yes, there are quite a few) compute the entire list of matches prior to returning the first match.

            - tye (but my friends call me "Tye")