in reply to Re: Pulling a file with a given extension
in thread Pulling a file with a given extension

Be aware though that a glob might be slower than a loop:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw( cmpthese ); sub globber { return(( glob '*.xml')[0]); } sub opener { opendir my $dir, '.'; my $file; while (defined($file= readdir $dir)) { last if $file=~ /\.xml$/; } closedir $dir; return $file; } cmpthese(5000, { globber => \&globber, opener => \&opener, });
          Rate globber  opener
globber  654/s      --    -76%
opener  2688/s    311%      --

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re^3: Pulling a file with a given extension
by Tanktalus (Canon) on Oct 18, 2005 at 18:46 UTC

    That all depends ... on whether you manage to get the opener to work ;-)

    I'd rather go with a slow function that was fast to write than a fast function that was slow and error prone to write. I can always speed up working code (or, failing that, fall back to the original slow code) much easier than taking fast code and making it work ;-)

    (Note - this is in no way intended to say that the above code doesn't work, only that I've seen many people get fooled when trying to use the *dir functions - including myself.)