globIn list context, returns a (possibly empty) list of filename expansions onthe value of EXPR such as the standard Unix shell /bin/csh would do. Inscalar context, glob iterates through such filename expansions, returningundef when the list is exhausted. This is the internal functionimplementing the <*.c>
operator, but you can use it directly. IfEXPR is omitted, $_
is used. The <*.c>
operator is discussed inmore detail in I/O Operators in perlop.
Note that glob
splits its arguments on whitespace and treatseach segment as separate pattern. As such, glob("*.c *.h")
matches all files with a .c or .h extension. The expressionglob(".* *")
matches all files in the current working directory.If you want to glob filenames that might contain whitespace, you'llhave to use extra quotes around the spacey filename to protect it.For example, to glob filenames that have an e
followed by a spacefollowed by an f
, use either of:
- @spacies = <"*e f*">;
- @spacies = glob '"*e f*"';
- @spacies = glob q("*e f*");
If you had to get a variable through, you could do this:
- @spacies = glob "'*${var}e f*'";
- @spacies = glob qq("*${var}e f*");
If non-empty braces are the only wildcard characters used in theglob
, no filenames are matched, but potentially many stringsare returned. For example, this produces nine strings, one foreach pairing of fruits and colors:
- @many = glob "{apple,tomato,cherry}={green,yellow,red}";
Beginning with v5.6.0, this operator is implemented using the standardFile::Glob
extension. See File::Glob for details, includingbsd_glob
which does not treat whitespace as a pattern separator.
Portability issues: glob in perlport.