8 # NOTE: The glob() export is only here for compatibility with 5.6.0.
9 # csh_glob() should not be used directly, unless you know what you're doing.
32 $EXPORT_TAGS{bsd_glob} = [@{$EXPORT_TAGS{glob}}];
34 our @EXPORT_OK = (@{$EXPORT_TAGS{'glob'}}, 'csh_glob');
36 our $VERSION = '1.37';
40 local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
41 Exporter::import(grep {
44 $DEFAULT_FLAGS &= ~GLOB_NOCASE()
46 elsif ($_ eq ':nocase') {
47 $DEFAULT_FLAGS |= GLOB_NOCASE();
49 elsif ($_ eq ':globally') {
50 no warnings 'redefine';
51 *CORE::GLOBAL::glob = \&File::Glob::csh_glob;
53 elsif ($_ eq ':bsd_glob') {
54 no strict; *{caller."::glob"} = \&bsd_glob_override;
66 $DEFAULT_FLAGS = GLOB_CSH();
67 if ($^O =~ /^(?:MSWin32|VMS|os2|riscos)$/) {
68 $DEFAULT_FLAGS |= GLOB_NOCASE();
76 File::Glob - Perl extension for BSD glob routine
80 use File::Glob ':bsd_glob';
82 @list = bsd_glob('*.[ch]');
83 $homedir = bsd_glob('~gnat', GLOB_TILDE | GLOB_ERR);
86 # an error occurred reading $homedir
89 ## override the core glob (CORE::glob() does this automatically
90 ## by default anyway, since v5.6.0)
91 use File::Glob ':globally';
92 my @sources = <*.{c,h,y}>;
94 ## override the core glob, forcing case sensitivity
95 use File::Glob qw(:globally :case);
96 my @sources = <*.{c,h,y}>;
98 ## override the core glob forcing case insensitivity
99 use File::Glob qw(:globally :nocase);
100 my @sources = <*.{c,h,y}>;
102 ## glob on all files in home directory
103 use File::Glob ':globally';
104 my @sources = <~gnat/*>;
108 The glob angle-bracket operator C<< <> >> is a pathname generator that
109 implements the rules for file name pattern matching used by Unix-like shells
110 such as the Bourne shell or C shell.
112 File::Glob::bsd_glob() implements the FreeBSD glob(3) routine, which is
113 a superset of the POSIX glob() (described in IEEE Std 1003.2 "POSIX.2").
114 bsd_glob() takes a mandatory C<pattern> argument, and an optional
115 C<flags> argument, and returns a list of filenames matching the
116 pattern, with interpretation of the pattern modified by the C<flags>
119 Since v5.6.0, Perl's CORE::glob() is implemented in terms of bsd_glob().
120 Note that they don't share the same prototype--CORE::glob() only accepts
121 a single argument. Due to historical reasons, CORE::glob() will also
122 split its argument on whitespace, treating it as multiple patterns,
123 whereas bsd_glob() considers them as one pattern. But see C<:bsd_glob>
124 under L</EXPORTS>, below.
126 =head2 META CHARACTERS
128 \ Quote the next metacharacter
131 * Match any string of characters
132 ? Match any single character
133 ~ User name home directory
135 The metanotation C<a{b,c,d}e> is a shorthand for C<abe ace ade>. Left to
136 right order is preserved, with results of matches being sorted separately
137 at a low level to preserve this order. As a special case C<{>, C<}>, and
138 C<{}> are passed undisturbed.
142 See also the L</POSIX FLAGS> below, which can be exported individually.
146 The C<:bsd_glob> export tag exports bsd_glob() and the constants listed
147 below. It also overrides glob() in the calling package with one that
148 behaves like bsd_glob() with regard to spaces (the space is treated as part
149 of a file name), but supports iteration in scalar context; i.e., it
150 preserves the core function's feature of returning the next item each time
155 The C<:glob> tag, now discouraged, is the old version of C<:bsd_glob>. It
156 exports the same constants and functions, but its glob() override does not
157 support iteration; it returns the last file name in scalar context. That
158 means this will loop forever:
160 use File::Glob ':glob';
161 while (my $file = <* copy.txt>) {
167 This function, which is included in the two export tags listed above,
168 takes one or two arguments. The first is the glob pattern. The
169 second, if given, is a set of flags ORed together. The available
170 flags and the default set of flags are listed below under L</POSIX FLAGS>.
172 Remember that to use the named constants for flags you must import
173 them, for example with C<:bsd_glob> described above. If not imported,
174 and C<use strict> is not in effect, then the constants will be
175 treated as bareword strings, which won't do what you what.
178 =head3 C<:nocase> and C<:case>
180 These two export tags globally modify the default flags that bsd_glob()
181 and, except on VMS, Perl's built-in C<glob> operator use. C<GLOB_NOCASE>
182 is turned on or off, respectively.
186 The csh_glob() function can also be exported, but you should not use it
187 directly unless you really know what you are doing. It splits the pattern
188 into words and feeds each one to bsd_glob(). Perl's own glob() function
189 uses this internally.
193 If no flags argument is give then C<GLOB_CSH> is set, and on VMS and
194 Windows systems, C<GLOB_NOCASE> too. Otherwise the flags to use are
195 determined solely by the flags argument. The POSIX defined flags are:
201 Force bsd_glob() to return an error when it encounters a directory it
202 cannot open or read. Ordinarily bsd_glob() continues to find matches.
206 Make bsd_glob() return an error (GLOB_NOSPACE) when the pattern expands
207 to a size bigger than the system constant C<ARG_MAX> (usually found in
208 limits.h). If your system does not define this constant, bsd_glob() uses
209 C<sysconf(_SC_ARG_MAX)> or C<_POSIX_ARG_MAX> where available (in that
210 order). You can inspect these values using the standard C<POSIX>
215 Each pathname that is a directory that matches the pattern has a slash
220 By default, file names are assumed to be case sensitive; this flag
221 makes bsd_glob() treat case differences as not significant.
223 =item C<GLOB_NOCHECK>
225 If the pattern does not match any pathname, then bsd_glob() returns a list
226 consisting of only the pattern. If C<GLOB_QUOTE> is set, its effect
227 is present in the pattern returned.
231 By default, the pathnames are sorted in ascending ASCII order; this
232 flag prevents that sorting (speeding up bsd_glob()).
236 The FreeBSD extensions to the POSIX standard are the following flags:
242 Pre-process the string to expand C<{pat,pat,...}> strings like csh(1).
243 The pattern '{}' is left unexpanded for historical reasons (and csh(1)
244 does the same thing to ease typing of find(1) patterns).
246 =item C<GLOB_NOMAGIC>
248 Same as C<GLOB_NOCHECK> but it only returns the pattern if it does not
249 contain any of the special characters "*", "?" or "[". C<NOMAGIC> is
250 provided to simplify implementing the historic csh(1) globbing
251 behaviour and should probably not be used anywhere else.
255 Use the backslash ('\') character for quoting: every occurrence of a
256 backslash followed by a character in the pattern is replaced by that
257 character, avoiding any special interpretation of the character.
258 (But see below for exceptions on DOSISH systems).
262 Expand patterns that start with '~' to user name home directories.
266 For convenience, C<GLOB_CSH> is a synonym for
267 C<GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE | GLOB_ALPHASORT>.
271 The POSIX provided C<GLOB_APPEND>, C<GLOB_DOOFFS>, and the FreeBSD
272 extensions C<GLOB_ALTDIRFUNC>, and C<GLOB_MAGCHAR> flags have not been
273 implemented in the Perl version because they involve more complex
274 interaction with the underlying C structures.
276 The following flag has been added in the Perl implementation for
281 =item C<GLOB_ALPHASORT>
283 If C<GLOB_NOSORT> is not in effect, sort filenames is alphabetical
284 order (case does not matter) rather than in ASCII order.
290 bsd_glob() returns a list of matching paths, possibly zero length. If an
291 error occurred, &File::Glob::GLOB_ERROR will be non-zero and C<$!> will be
292 set. &File::Glob::GLOB_ERROR is guaranteed to be zero if no error occurred,
293 or one of the following values otherwise:
297 =item C<GLOB_NOSPACE>
299 An attempt to allocate memory failed.
303 The glob was stopped because an error was encountered.
307 In the case where bsd_glob() has found some matching paths, but is
308 interrupted by an error, it will return a list of filenames B<and>
309 set &File::Glob::ERROR.
311 Note that bsd_glob() deviates from POSIX and FreeBSD glob(3) behaviour
312 by not considering C<ENOENT> and C<ENOTDIR> as errors - bsd_glob() will
313 continue processing despite those errors, unless the C<GLOB_ERR> flag is
316 Be aware that all filenames returned from File::Glob are tainted.
324 If you want to use multiple patterns, e.g. C<bsd_glob("a* b*")>, you should
325 probably throw them in a set as in C<bsd_glob("{a*,b*}")>. This is because
326 the argument to bsd_glob() isn't subjected to parsing by the C shell.
327 Remember that you can use a backslash to escape things.
331 On DOSISH systems, backslash is a valid directory separator character.
332 In this case, use of backslash as a quoting character (via GLOB_QUOTE)
333 interferes with the use of backslash as a directory separator. The
334 best (simplest, most portable) solution is to use forward slashes for
335 directory separators, and backslashes for quoting. However, this does
336 not match "normal practice" on these systems. As a concession to user
337 expectation, therefore, backslashes (under GLOB_QUOTE) only quote the
338 glob metacharacters '[', ']', '{', '}', '-', '~', and backslash itself.
339 All other backslashes are passed through unchanged.
343 Win32 users should use the real slash. If you really want to use
344 backslashes, consider using Sarathy's File::DosGlob, which comes with
345 the standard Perl distribution.
351 L<perlfunc/glob>, glob(3)
355 The Perl interface was written by Nathan Torkington E<lt>gnat@frii.comE<gt>,
356 and is released under the artistic license. Further modifications were
357 made by Greg Bacon E<lt>gbacon@cs.uah.eduE<gt>, Gurusamy Sarathy
358 E<lt>gsar@activestate.comE<gt>, and Thomas Wegner
359 E<lt>wegner_thomas@yahoo.comE<gt>. The C glob code has the
362 Copyright (c) 1989, 1993 The Regents of the University of California.
365 This code is derived from software contributed to Berkeley by
368 Redistribution and use in source and binary forms, with or without
369 modification, are permitted provided that the following conditions
376 Redistributions of source code must retain the above copyright
377 notice, this list of conditions and the following disclaimer.
381 Redistributions in binary form must reproduce the above copyright
382 notice, this list of conditions and the following disclaimer in the
383 documentation and/or other materials provided with the distribution.
387 Neither the name of the University nor the names of its contributors
388 may be used to endorse or promote products derived from this software
389 without specific prior written permission.
393 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
394 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
395 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
396 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
397 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
398 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
399 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
400 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
401 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
402 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF