Another option would be to skip the glob operator altogether, and use (open|read|close)dir instead.

This would skip the odd glob behavior, and also get around some other glob() limitations you might hit later. ( like "too many args").

Here's my shot at converting your short example:

for (my $i=0; $i < 5; $i++){ opendir(DIR,$i) or die(); foreach my $file (readdir(DIR)) { # if the filename ends in "test.txt"... # adjust the regex as needed if ($file =~ /test\.txt$/) { print "$i, -${file}-\n"; } } closedir(DIR); }
Update:per scain's comment, it looks like perl's glob was updated in 5.6.0 to use an internal routine on most implementations. OTOH, it looks like p5p is saying they will tie it to File::Glob, which sucks in Exporter, etc, probably slowing it down.

As far as which one is faster, using the short example, readdir() is at least twice as fast on perl 5.6.0 and hundreds of times faster on perl 5.5.003 ( where glob() still calls out to csh). Here's what I tried, after making a few test directories and files:

use Benchmark; timethese(10000, { 'readdir' => sub { my @blah; for (my $i=0; $i < 5; $i++){ opendir(DIR,$i) or die(); foreach my $file (readdir(DIR)) { if ($file =~ /test.txt$/) { push(@blah,$file); } } closedir(DIR); } }, 'glob' => sub { my @blah; for (my $i=0; $i < 5; $i++){ for (<$i/*test.txt>) { push(@blah,$_); } } } } ); Results on my perl 5.6.x box: Benchmark: timing 10000 iterations of glob, readdir... glob: 12 wallclock secs ( 3.24 usr + 2.22 sys = 5.46 CPU) @ 1831.50/s (n=10000) readdir: 3 wallclock secs ( 1.54 usr + 1.69 sys = 3.23 CPU) @ 3095.98/s (n=10000) on a 5.005_003 box (1000 iterations, because it's so slow): Benchmark: timing 1000 iterations of glob, readdir... glob: 216 wallclock secs ( 1.83 usr 9.21 sys + 63.22 cusr 107.38 csys = 0.00 CPU) readdir: 1 wallclock secs ( 0.43 usr + 0.37 sys = 0.80 CPU)

In reply to Re: More Fileglob in scalar context question by kschwab
in thread More Fileglob in scalar context question by scain

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.