4 our($VERSION, @ISA, @EXPORT_OK, @EXPORT_FAIL, %EXPORT_TAGS,
5 $AUTOLOAD, $DEFAULT_FLAGS);
9 @ISA = qw(Exporter AutoLoader);
11 # NOTE: The glob() export is only here for compatibility with 5.6.0.
12 # csh_glob() should not be used directly, unless you know what you're doing.
63 if ($_[$i] =~ /^:(case|nocase|globally)$/) {
65 $DEFAULT_FLAGS &= ~GLOB_NOCASE() if $1 eq 'case';
66 $DEFAULT_FLAGS |= GLOB_NOCASE() if $1 eq 'nocase';
67 if ($1 eq 'globally') {
69 *CORE::GLOBAL::glob = \&File::Glob::csh_glob;
75 goto &Exporter::import;
79 # This AUTOLOAD is used to 'autoload' constants from the constant()
80 # XS function. If a constant is not found then control is passed
81 # to the AUTOLOAD in AutoLoader.
84 ($constname = $AUTOLOAD) =~ s/.*:://;
85 my $val = constant($constname, @_ ? $_[0] : 0);
87 if ($! =~ /Invalid/ || $!{EINVAL}) {
89 $AutoLoader::AUTOLOAD = $AUTOLOAD;
90 goto &AutoLoader::AUTOLOAD;
94 Carp::croak("Your vendor has not defined File::Glob macro $constname");
97 eval "sub $AUTOLOAD { $val }";
101 XSLoader::load 'File::Glob', $VERSION;
103 # Preloaded methods go here.
106 return constant('GLOB_ERROR', 0);
117 $DEFAULT_FLAGS = GLOB_CSH();
118 if ($^O =~ /^(?:MSWin32|VMS|os2|dos|riscos|MacOS)$/) {
119 $DEFAULT_FLAGS |= GLOB_NOCASE();
122 # Autoload methods go after =cut, and are processed by the autosplit program.
125 my ($pat,$flags) = @_;
126 $flags = $DEFAULT_FLAGS if @_ < 2;
127 return doglob($pat,$flags);
130 # File::Glob::glob() is deprecated because its prototype is different from
131 # CORE::glob() (use bsd_glob() instead)
136 ## borrowed heavily from gsar's File::DosGlob
145 # glob without args defaults to $_
146 $pat = $_ unless defined $pat;
149 $pat =~ s/^\s+//; # Protect against empty elements in
150 $pat =~ s/\s+$//; # things like < *.c> and <*.c >.
151 # These alone shouldn't trigger ParseWords.
153 # XXX this is needed for compatibility with the csh
154 # implementation in Perl. Need to support a flag
155 # to disable this behavior.
156 require Text::ParseWords;
157 @pat = Text::ParseWords::parse_line('\s+',0,$pat);
160 # assume global context if not provided one
161 $cxix = '_G_' unless defined $cxix;
162 $iter{$cxix} = 0 unless exists $iter{$cxix};
164 # if we're just beginning, do it all first
165 if ($iter{$cxix} == 0) {
167 $entries{$cxix} = [ map { doglob($_, $DEFAULT_FLAGS) } @pat ];
170 $entries{$cxix} = [ doglob($pat, $DEFAULT_FLAGS) ];
174 # chuck it all out, quick or slow
177 return @{delete $entries{$cxix}};
180 if ($iter{$cxix} = scalar @{$entries{$cxix}}) {
181 return shift @{$entries{$cxix}};
184 # return undef for EOL
186 delete $entries{$cxix};
197 File::Glob - Perl extension for BSD glob routine
201 use File::Glob ':glob';
202 @list = bsd_glob('*.[ch]');
203 $homedir = bsd_glob('~gnat', GLOB_TILDE | GLOB_ERR);
205 # an error occurred reading $homedir
208 ## override the core glob (CORE::glob() does this automatically
209 ## by default anyway, since v5.6.0)
210 use File::Glob ':globally';
211 my @sources = <*.{c,h,y}>
213 ## override the core glob, forcing case sensitivity
214 use File::Glob qw(:globally :case);
215 my @sources = <*.{c,h,y}>
217 ## override the core glob forcing case insensitivity
218 use File::Glob qw(:globally :nocase);
219 my @sources = <*.{c,h,y}>
223 File::Glob::bsd_glob() implements the FreeBSD glob(3) routine, which is
224 a superset of the POSIX glob() (described in IEEE Std 1003.2 "POSIX.2").
225 bsd_glob() takes a mandatory C<pattern> argument, and an optional
226 C<flags> argument, and returns a list of filenames matching the
227 pattern, with interpretation of the pattern modified by the C<flags>
230 Since v5.6.0, Perl's CORE::glob() is implemented in terms of bsd_glob().
231 Note that they don't share the same prototype--CORE::glob() only accepts
232 a single argument. Due to historical reasons, CORE::glob() will also
233 split its argument on whitespace, treating it as multiple patterns,
234 whereas bsd_glob() considers them as one pattern.
236 The POSIX defined flags for bsd_glob() are:
242 Force bsd_glob() to return an error when it encounters a directory it
243 cannot open or read. Ordinarily bsd_glob() continues to find matches.
247 Each pathname that is a directory that matches the pattern has a slash
252 By default, file names are assumed to be case sensitive; this flag
253 makes bsd_glob() treat case differences as not significant.
255 =item C<GLOB_NOCHECK>
257 If the pattern does not match any pathname, then bsd_glob() returns a list
258 consisting of only the pattern. If C<GLOB_QUOTE> is set, its effect
259 is present in the pattern returned.
263 By default, the pathnames are sorted in ascending ASCII order; this
264 flag prevents that sorting (speeding up bsd_glob()).
268 The FreeBSD extensions to the POSIX standard are the following flags:
274 Pre-process the string to expand C<{pat,pat,...}> strings like csh(1).
275 The pattern '{}' is left unexpanded for historical reasons (and csh(1)
276 does the same thing to ease typing of find(1) patterns).
278 =item C<GLOB_NOMAGIC>
280 Same as C<GLOB_NOCHECK> but it only returns the pattern if it does not
281 contain any of the special characters "*", "?" or "[". C<NOMAGIC> is
282 provided to simplify implementing the historic csh(1) globbing
283 behaviour and should probably not be used anywhere else.
287 Use the backslash ('\') character for quoting: every occurrence of a
288 backslash followed by a character in the pattern is replaced by that
289 character, avoiding any special interpretation of the character.
290 (But see below for exceptions on DOSISH systems).
294 Expand patterns that start with '~' to user name home directories.
298 For convenience, C<GLOB_CSH> is a synonym for
299 C<GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE | GLOB_ALPHASORT>.
303 The POSIX provided C<GLOB_APPEND>, C<GLOB_DOOFFS>, and the FreeBSD
304 extensions C<GLOB_ALTDIRFUNC>, and C<GLOB_MAGCHAR> flags have not been
305 implemented in the Perl version because they involve more complex
306 interaction with the underlying C structures.
308 The following flag has been added in the Perl implementation for
313 =item C<GLOB_ALPHASORT>
315 If C<GLOB_NOSORT> is not in effect, sort filenames is alphabetical
316 order (case does not matter) rather than in ASCII order.
322 bsd_glob() returns a list of matching paths, possibly zero length. If an
323 error occurred, &File::Glob::GLOB_ERROR will be non-zero and C<$!> will be
324 set. &File::Glob::GLOB_ERROR is guaranteed to be zero if no error occurred,
325 or one of the following values otherwise:
329 =item C<GLOB_NOSPACE>
331 An attempt to allocate memory failed.
335 The glob was stopped because an error was encountered.
339 In the case where bsd_glob() has found some matching paths, but is
340 interrupted by an error, it will return a list of filenames B<and>
341 set &File::Glob::ERROR.
343 Note that bsd_glob() deviates from POSIX and FreeBSD glob(3) behaviour
344 by not considering C<ENOENT> and C<ENOTDIR> as errors - bsd_glob() will
345 continue processing despite those errors, unless the C<GLOB_ERR> flag is
348 Be aware that all filenames returned from File::Glob are tainted.
356 If you want to use multiple patterns, e.g. C<bsd_glob "a* b*">, you should
357 probably throw them in a set as in C<bsd_glob "{a*,b*}">. This is because
358 the argument to bsd_glob() isn't subjected to parsing by the C shell.
359 Remember that you can use a backslash to escape things.
363 On DOSISH systems, backslash is a valid directory separator character.
364 In this case, use of backslash as a quoting character (via GLOB_QUOTE)
365 interferes with the use of backslash as a directory separator. The
366 best (simplest, most portable) solution is to use forward slashes for
367 directory separators, and backslashes for quoting. However, this does
368 not match "normal practice" on these systems. As a concession to user
369 expectation, therefore, backslashes (under GLOB_QUOTE) only quote the
370 glob metacharacters '[', ']', '{', '}', '-', '~', and backslash itself.
371 All other backslashes are passed through unchanged.
375 Win32 users should use the real slash. If you really want to use
376 backslashes, consider using Sarathy's File::DosGlob, which comes with
377 the standard Perl distribution.
381 Mac OS (Classic) users should note a few differences. Since
382 Mac OS is not Unix, when the glob code encounters a tilde glob (e.g.
383 ~user/foo) and the C<GLOB_TILDE> flag is used, it simply returns that
384 pattern without doing any expansion.
386 Glob on Mac OS is case-insensitive by default (if you don't use any
387 flags). If you specify any flags at all and still want glob
388 to be case-insensitive, you must include C<GLOB_NOCASE> in the flags.
390 The path separator is ':' (aka colon), not '/' (aka slash). Mac OS users
391 should be careful about specifying relative pathnames. While a full path
392 always begins with a volume name, a relative pathname should always
393 begin with a ':'. If specifying a volume name only, a trailing ':' is
400 The Perl interface was written by Nathan Torkington E<lt>gnat@frii.comE<gt>,
401 and is released under the artistic license. Further modifications were
402 made by Greg Bacon E<lt>gbacon@cs.uah.eduE<gt>, Gurusamy Sarathy
403 E<lt>gsar@activestate.comE<gt>, and Thomas Wegner
404 E<lt>wegner_thomas@yahoo.comE<gt>. The C glob code has the
407 Copyright (c) 1989, 1993 The Regents of the University of California.
410 This code is derived from software contributed to Berkeley by
413 Redistribution and use in source and binary forms, with or without
414 modification, are permitted provided that the following conditions
417 1. Redistributions of source code must retain the above copyright
418 notice, this list of conditions and the following disclaimer.
419 2. Redistributions in binary form must reproduce the above copyright
420 notice, this list of conditions and the following disclaimer in the
421 documentation and/or other materials provided with the distribution.
422 3. Neither the name of the University nor the names of its contributors
423 may be used to endorse or promote products derived from this software
424 without specific prior written permission.
426 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
427 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
428 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
429 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
430 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
431 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
432 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
433 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
434 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
435 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF