CARVIEW |
- #glob EXPR
- #glob
-
In list context, returns a (possibly empty) list of filename expansions on the value of EXPR such as the Unix shell Bash would do. In scalar context, glob iterates through such filename expansions, returning
undef
when the list is exhausted. If EXPR is omitted,$_
is used.# List context my @txt_files = glob("*.txt"); my @perl_files = glob("*.pl *.pm"); # Scalar context while (my $file = glob("*.mp3")) { # Do stuff }
Glob also supports an alternate syntax using
<
>
as delimiters. While this syntax is supported, it is recommended that you useglob
instead as it is more readable and searchable.my @txt_files = <"*.txt">;
If you need case insensitive file globbing that can be achieved using the
:nocase
parameter of thebsd_glob
module.use File::Glob qw(:globally :nocase); my @txt = glob("readme*"); # README readme.txt Readme.md
Note that
glob
splits its arguments on whitespace and treats each 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'll have to use extra quotes around the spacey filename to protect it. For example, to glob filenames that have ane
followed by a space followed by anf
, use one of:my @spacies = <"*e f*">; my @spacies = glob('"*e f*"'); my @spacies = glob(q("*e f*"));
If you had to get a variable through, you could do this:
my @spacies = glob("'*${var}e f*'"); my @spacies = glob(qq("*${var}e f*"));
If non-empty braces are the only wildcard characters used in the
glob
, no filenames are matched, but potentially many strings are returned. For example, this produces nine strings, one for each pairing of fruits and colors:my @many = glob("{apple,tomato,cherry}={green,yellow,red}");
This operator is implemented using the standard
File::Glob
extension. Seebsd_glob
for details, includingbsd_glob
, which does not treat whitespace as a pattern separator.If a
glob
expression is used as the condition of awhile
orfor
loop, then it will be implicitly assigned to$_
. If either aglob
expression or an explicit assignment of aglob
expression to a scalar is used as awhile
/for
condition, then the condition actually tests for definedness of the expression's value, not for its regular truth value.Internal implementation details:
This is the internal function implementing the
<*.c>
operator, but you can use it directly. The<*.c>
operator is discussed in more detail in "I/O Operators" in perlop.Portability issues: "glob" in perlport.
Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via the GitHub issue tracker or email regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the Perl issue tracker, the mailing list, or IRC to report any issues with the contents or format of the documentation.