File::Glob(3pm)
๐ธ NAME ๐ธ
okay bestie, this is File::Glob(3pm) from the Perl Programmers Reference Guide, and the whole vibe is File::Glob โ Perl extension for BSD glob routine. ๐
Itโs the Perl wrapper around the BSD globbing routine, and yes, itโs giving โtiny module, big shell-energy.โ
โจ SYNOPSIS โจ
use File::Glob ':bsd_glob';
@list = bsd_glob('*.[ch]');
$homedir = bsd_glob('~gnat', GLOB_TILDE | GLOB_ERR);
if (GLOB_ERROR) {
# an error occurred reading $homedir
}
## override the core glob (CORE::glob() does this automatically
## by default anyway, since v5.6.0)
use File::Glob ':globally';
my @sources = <*.{c,h,y}>;
## override the core glob, forcing case sensitivity
use File::Glob qw(:globally :case);
my @sources = <*.{c,h,y}>;
## override the core glob forcing case insensitivity
use File::Glob qw(:globally :nocase);
my @sources = <*.{c,h,y}>;
## glob on all files in home directory
use File::Glob ':globally';
my @sources = <~gnat/*>;
๐ฆ DESCRIPTION ๐ฆ
The glob angle-bracket operator <> is a pathname generator that implements the rules for file name pattern matching used by Unix-like shells such as the Bourne shell or C shell. ๐โจ
File::Glob::bsd_glob() implements the FreeBSD glob(3) routine, which is a superset of the POSIX glob() (described in IEEE Std 1003.2 "POSIX.2"). bsd_glob() takes a mandatory pattern argument, and an optional flags argument, and returns a list of filenames matching the pattern, with interpretation of the pattern modified by the flags variable. ๐
Since v5.6.0, Perlโs CORE::glob() is implemented in terms of bsd_glob(). Note that they donโt share the same prototype--CORE::glob() only accepts a single argument. Due to historical reasons, CORE::glob() will also split its argument on whitespace, treating it as multiple patterns, whereas bsd_glob() considers them as one pattern. But see :bsd_glob under โEXPORTSโ, below. ๐
๐ฎ META CHARACTERS ๐ฎ
\โ quote the next metacharacter[]โ character class{}โ multiple pattern*โ match any string of characters?โ match any single character~โ user name home directory
The metanotation a{b,c,d}e is a shorthand for abe ace ade. Left to right order is preserved, with results of matches being sorted separately at a low level to preserve this order. As a special case {, }, and {} are passed undisturbed. ๐ซถ
๐ EXPORTS ๐
See also the POSIX FLAGS below, which can be exported individually.
๐ :bsd_glob
The :bsd_glob export tag exports bsd_glob() and the constants listed below. It also overrides glob() in the calling package with one that behaves like bsd_glob() with regard to spaces (the space is treated as part of a file name), but supports iteration in scalar context; i.e., it preserves the core functionโs feature of returning the next item each time it is called. โจ
๐ :glob
The :glob tag, now discouraged, is the old version of :bsd_glob. It exports the same constants and functions, but its glob() override does not support iteration; it returns the last file name in scalar context. That means this will loop forever: ๐
use File::Glob ':glob';
while (my $file = <* copy.txt>) {
...
}
๐ bsd_glob
This function, which is included in the two export tags listed above, takes one or two arguments. The first is the glob pattern. The second, if given, is a set of flags ORed together. The available flags and the default set of flags are listed below under POSIX FLAGS.
Remember that to use the named constants for flags you must import them, for example with :bsd_glob described above. If not imported, and use strict is not in effect, then the constants will be treated as bareword strings, which wonโt do what you want. ๐ญ
๐ฆ :nocase and :case
These two export tags globally modify the default flags that bsd_glob() and, except on VMS, Perlโs built-in glob operator use. GLOB_NOCASE is turned on or off, respectively.
๐
csh_glob
The csh_glob() function can also be exported, but you should not use it directly unless you really know what you are doing. It splits the pattern into words and feeds each one to bsd_glob(). Perlโs own glob() function uses this internally. ๐
๐ธ POSIX FLAGS ๐ธ
If no flags argument is given then GLOB_CSH is set, and on VMS and Windows systems, GLOB_NOCASE too. Otherwise the flags to use are determined solely by the flags argument. The POSIX defined flags are:
GLOB_ERRโ forcebsd_glob()to return an error when it encounters a directory it cannot open or read. Ordinarilybsd_glob()continues to find matches.GLOB_LIMITโ makebsd_glob()return an error (GLOB_NOSPACE) when the pattern expands to a size bigger than the system constantARG_MAX(usually found inlimits.h). If your system does not define this constant,bsd_glob()usessysconf(_SC_ARG_MAX)or_POSIX_ARG_MAXwhere available (in that order). You can inspect these values using the standardPOSIXextension.GLOB_MARKโ each pathname that is a directory that matches the pattern has a slash appended.GLOB_NOCASEโ by default, file names are assumed to be case sensitive; this flag makesbsd_glob()treat case differences as not significant.GLOB_NOCHECKโ if the pattern does not match any pathname, thenbsd_glob()returns a list consisting of only the pattern. IfGLOB_QUOTEis set, its effect is present in the pattern returned.GLOB_NOSORTโ by default, the pathnames are sorted in ascending ASCII order; this flag prevents that sorting (speeding upbsd_glob()).
The FreeBSD extensions to the POSIX standard are the following flags:
GLOB_BRACEโ pre-process the string to expand{pat,pat,...}strings likecsh(1). The pattern'{}'is left unexpanded for historical reasons (andcsh(1)does the same thing to ease typing offind(1)patterns).GLOB_NOMAGICโ same asGLOB_NOCHECKbut it only returns the pattern if it does not contain any of the special characters"*","?"or"[".NOMAGICis provided to simplify implementing the historiccsh(1)globbing behaviour and should probably not be used anywhere else.GLOB_QUOTEโ use the backslash (\) character for quoting: every occurrence of a backslash followed by a character in the pattern is replaced by that character, avoiding any special interpretation of the character. (But see below for exceptions onDOSISHsystems).GLOB_TILDEโ expand patterns that start with'~'to user name home directories.GLOB_CSHโ for convenience,GLOB_CSHis a synonym forGLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE | GLOB_ALPHASORT.
The POSIX provided GLOB_APPEND, GLOB_DOOFFS, and the FreeBSD extensions GLOB_ALTDIRFUNC, and GLOB_MAGCHAR flags have not been implemented in the Perl version because they involve more complex interaction with the underlying C structures. ๐
The following flag has been added in the Perl implementation for csh compatibility:
GLOB_ALPHASORTโ ifGLOB_NOSORTis not in effect, sort filenames in alphabetical order (case does not matter) rather than in ASCII order.
๐ DIAGNOSTICS ๐
bsd_glob() returns a list of matching paths, possibly zero length. If an error occurred, &File::Glob::GLOB_ERROR will be non-zero and $! will be set. &File::Glob::GLOB_ERROR is guaranteed to be zero if no error occurred, or one of the following values otherwise:
GLOB_NOSPACEโ an attempt to allocate memory failed.GLOB_ABENDโ the glob was stopped because an error was encountered.
In the case where bsd_glob() has found some matching paths, but is interrupted by an error, it will return a list of filenames and set &File::Glob::ERROR. ๐ญ
Note that bsd_glob() deviates from POSIX and FreeBSD glob(3) behaviour by not considering ENOENT and ENOTDIR as errors - bsd_glob() will continue processing despite those errors, unless the GLOB_ERR flag is set.
Be aware that all filenames returned from File::Glob are tainted. ๐จ
๐ NOTES ๐
- If you want to use multiple patterns, e.g.
bsd_glob("a* b*"), you should probably throw them in a set as inbsd_glob("{a*,b*}"). This is because the argument tobsd_glob()isnโt subjected to parsing by the C shell. Remember that you can use a backslash to escape things. - On
DOSISHsystems, backslash is a valid directory separator character. In this case, use of backslash as a quoting character (viaGLOB_QUOTE) interferes with the use of backslash as a directory separator. The best (simplest, most portable) solution is to use forward slashes for directory separators, and backslashes for quoting. However, this does not match"normal practice"on these systems. As a concession to user expectation, therefore, backslashes (underGLOB_QUOTE) only quote the glob metacharacters'[',']','{','}','-','~', and backslash itself. All other backslashes are passed through unchanged. - Win32 users should use the real slash. If you really want to use backslashes, consider using Sarathyโs
File::DosGlob, which comes with the standard Perl distribution. ๐ชโจ
๐ SEE ALSO ๐
glob in perlfunc, glob(3)
๐ AUTHOR ๐
Ok so this whole Perl interface? Written by Nathan Torkington <gnat@frii.com>, and it's released under the artistic license. Slay.
Then some absolute icons came through with further modifications, and we're gonna thank them by name because that's just good manners:
- Greg Bacon
<gbacon@cs.uah.edu> - Gurusamy Sarathy
<gsar@activestate.com> - Thomas Wegner
<wegner_thomas@yahoo.com>
And the C glob code underneath all of it? Yeah, that has its own copyright situation. Buckle up bestie, because:
Copyright (c) 1989, 1993 The Regents of the University of California.
All rights reserved.
This code is derived from software contributed to Berkeley by Guido van Rossum. (Yes, that Guido. main character energy fr fr ๐)
๐ the actual license stuff ๐
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
๐ quick vibe translation, because legalese is giving ancient scroll: keep the copyright notice if you ship the source, keep it in the docs if you ship a binary, and don't use the University's name or the contributors' names to promote your thing without asking first. That's it. That's the whole tea. ๐ต
And now the part where everything is IN ALL CAPS, which is how you know it's serious:
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
Real talk though: that block is a disclaimer, not a promise. No warranty, express or implied, at all โ including merchantability or fitness for a particular purpose. And nobody involved is liable for damages of any kind if something goes sideways. Read it, don't skim it, it's the fine print for a reason. ๐
๐ and that's the whole page, bestie!! ๐ You made it to the end, which is honestly iconic. This lil beauty is from perl v5.42.3, dated 2026-11-03 โ go forth, glob responsibly, and remember: keep the copyright notices, ship the disclaimer, and don't be weird about it. ๐โจ๐ฆ
๐ฆ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms