Perl Programming Documentation
Download Perl
Explore

Manual

- Overview
- Tutorials
- FAQs
- History / Changes
- License

Reference

- Language
- Functions
- Operators
- Special Variables
- Pragmas
- Utilities
- Internals
- Platform Specific

Modules

- A • B • C • D • E
- F • G • H • I • L
- M • N • O • P • S
- T • U • X

Tools

- Preferences

# glob

Perl 5 version 26.0 documentation
Go to top
Show recent pages
Recently read

# glob

Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage

- glob EXPR

- glob

  In list context, returns a (possibly empty) list of filename expansions on the value of EXPR such as the standard Unix shell /bin/csh would do. In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted. This is the internal function implementing the `<*.c>` operator, but you can use it directly. If EXPR is omitted, $_ is used. The `<*.c>` operator is discussed in more detail in I/O Operators in perlop.

  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 expression `glob(".* *")` 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 an `e` followed by a space followed by an `f` , 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. See File::Glob for details, including bsd_glob , which does not treat whitespace as a pattern separator.

  Portability issues: glob in perlport.