[tpm] Anomalous globular behaviour?

Uri Guttman uri at stemsystems.com
Sat Apr 25 14:24:22 PDT 2009


>>>>> "a" == arocker  <arocker at vex.net> writes:

  >> When looking at a flag file I use if (-e $file){...}
  >> 
  a> TMTOWTDI, and that actually looks more reasonable. I'd been dealing with a
  a> glob for other reasons, so it was still lying around when I looked for a
  a> tool.

  >> If the pattern does not match any pathname, then bsd_glob() returns a
  >> list consisting of only the pattern.
  >> 
  a> So if you read the small print on the tin, it does what it says. It's just
  a> almost but not exactly how the shell behaves, which was what I was
  a> expecting.

i almost never use globbing in perl. perl has much better patterns for
filtering and it becomes trivial with read_dir:

use File::Slurp ;

my $dir = 'foo' ;

my @files = grep -f, read_dir( $dir ) ;

and that was a tricky bug i made there. can you spot and correct it?


********SPOILER SPACE********





























the -f will actually fail on all the files as $dir isn't . and the $_
value is just the filename without the $dir prefix

so you should also prefix the dir with map (i intend to add this feature
to read_dir one day):

my @file_paths = grep -f, map "$dir/$_", read_dir( $dir ) ;

now those file paths are ready to read in or whatever

uri

-- 
Uri Guttman  ------  uri at stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


More information about the toronto-pm mailing list