12.2 Globbing
The command prompt usually takes a solitary asterisk (
The expansion of arguments like
*
or
*.c
into the list of matching filenames is called
globbing
. Perl supports globbing through a very simple mechanism - just put the globbing pattern between
angle brackets or use the more mnemonically named @a = <*.plx>; @a = glob("*.plx");
In a list context, as demonstrated here, the glob returns a list of all names that match the pattern or an empty list if none match. In a scalar context, the next name that matches is returned, or
while (defined($nextname = <c:/scripts/*.plx>)) { print "one of the files is $nextname\n"; } Here the returned filenames begin with c:\scripts\ , so that if you want just the last part of the name, you'll have to whittle it down yourself, like so: while ($nextname = <c:/scripts/*.plx>) { $nextname =~ s#.*/##; # remove part before last slash print "one of the files is $nextname\n"; } Multiple patterns are permitted inside the file glob argument; the lists are constructed separately and then concatenated as if they were one big list: @fred_barney_files = <fred* barney*>; In other words, the glob returns the same values that an equivalent dir /B command with the same parameters would return.
Although
file globbing and regular-expression matching function similarly, the meanings of their various special characters are quite different. Don't confuse the two, or you'll be wondering why The argument to glob is variable interpolated before expansion. You can use Perl variables to select a wildcard based on a string computed at runtime: if (-d "c:/tmp") { $where = "c:/tmp"; } else { $where = "c:/temp"; } @files = <$where/*>;
Here we set
There's one exception to this rule: the pattern
|
|