bsd_glob#

Expand a shell-style pattern and return the list of matching paths.

Synopsis#

my @list    = bsd_glob('*.[ch]');
my @homes   = bsd_glob('~gnat', GLOB_TILDE | GLOB_ERR);
my @latest  = bsd_glob('src/*.pl', GLOB_NOSORT);

What you get back#

A list of filenames, possibly empty. In scalar context you get the count of matches. Filenames are returned as plain strings in the order determined by the flags (alphabetical under the default GLOB_CSH, unsorted under GLOB_NOSORT).

When the pattern matches nothing and neither GLOB_NOCHECK nor GLOB_NOMAGIC is in effect, the list is empty. With GLOB_NOCHECK the original pattern is returned as the sole element; with GLOB_NOMAGIC the pattern is returned only when it contains no metacharacters.

Examples#

use File::Glob ':bsd_glob';
my @perl = bsd_glob('*.pl');          # ('a.pl', 'b.pl', ...)
## Brace expansion — default flags include GLOB_BRACE

my @src = bsd_glob('{lib,t}/*.pm');   # files under lib/ and t/
## Tilde expansion to another user's home

my ($home) = bsd_glob('~gnat', GLOB_TILDE);
## Case-insensitive match

my @readmes = bsd_glob('readme*', GLOB_NOCASE);

Edge cases#

  • No second argument: flags come from $File::Glob::DEFAULT_FLAGS (initialised to GLOB_CSH).

  • GLOB_APPEND, GLOB_DOOFFS, GLOB_ALTDIRFUNC, GLOB_MAGCHAR are masked out of the caller’s flags: they require C-level state pperl does not expose.

  • A pattern containing an embedded NUL byte returns the empty list.

  • Missing pattern (zero-argument call) returns the empty list.

Differences from upstream#

  • bsd_glob_override (installed as the caller’s glob by use File::Glob ':bsd_glob') is an alias for bsd_glob and does not implement the scalar-context iterator state machine upstream provides. In list context the behaviour matches; in scalar context upstream yields one name per call until exhausted, whereas pperl returns the match count. Covered by t/81-xs-native/File/Glob/*.t.

  • Filenames are not tainted. pperl does not implement taint mode.

See also#

  • csh_glob — the form Perl’s built-in glob uses internally.

  • GLOB_ERROR — non-zero after a traversal error.

  • GLOB_CSH — the default flag bundle.

  • GLOB_NOCHECK — return the pattern itself when nothing matches.