5 our($VERSION, @ISA, @EXPORT_OK, @EXPORT_FAIL, %EXPORT_TAGS,
6 $AUTOLOAD, $DEFAULT_FLAGS);
12 @ISA = qw(Exporter AutoLoader);
14 # NOTE: The glob() export is only here for compatibility with 5.6.0.
15 # 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/) {
88 $AutoLoader::AUTOLOAD = $AUTOLOAD;
89 goto &AutoLoader::AUTOLOAD;
92 croak "Your vendor has not defined File::Glob macro $constname";
95 eval "sub $AUTOLOAD { $val }";
99 XSLoader::load 'File::Glob', $VERSION;
101 # Preloaded methods go here.
104 return constant('GLOB_ERROR', 0);
107 sub GLOB_CSH () { GLOB_BRACE() | GLOB_NOMAGIC() | GLOB_QUOTE() | GLOB_TILDE() }
109 $DEFAULT_FLAGS = GLOB_CSH();
110 if ($^O =~ /^(?:MSWin32|VMS|os2|dos|riscos|MacOS)$/) {
111 $DEFAULT_FLAGS |= GLOB_NOCASE();
114 # Autoload methods go after =cut, and are processed by the autosplit program.
117 my ($pat,$flags) = @_;
118 $flags = $DEFAULT_FLAGS if @_ < 2;
119 return doglob($pat,$flags);
122 # File::Glob::glob() is deprecated because its prototype is different from
123 # CORE::glob() (use bsd_glob() instead)
128 ## borrowed heavily from gsar's File::DosGlob
137 # glob without args defaults to $_
138 $pat = $_ unless defined $pat;
141 $pat =~ s/^\s+//; # Protect against empty elements in
142 $pat =~ s/\s+$//; # things like < *.c> and <*.c >.
143 # These alone shouldn't trigger ParseWords.
145 # XXX this is needed for compatibility with the csh
146 # implementation in Perl. Need to support a flag
147 # to disable this behavior.
148 require Text::ParseWords;
149 @pat = Text::ParseWords::parse_line('\s+',0,$pat);
152 # assume global context if not provided one
153 $cxix = '_G_' unless defined $cxix;
154 $iter{$cxix} = 0 unless exists $iter{$cxix};
156 # if we're just beginning, do it all first
157 if ($iter{$cxix} == 0) {
159 $entries{$cxix} = [ map { doglob($_, $DEFAULT_FLAGS) } @pat ];
162 $entries{$cxix} = [ doglob($pat, $DEFAULT_FLAGS) ];
166 # chuck it all out, quick or slow
169 return @{delete $entries{$cxix}};
172 if ($iter{$cxix} = scalar @{$entries{$cxix}}) {
173 return shift @{$entries{$cxix}};
176 # return undef for EOL
178 delete $entries{$cxix};
189 File::Glob - Perl extension for BSD glob routine
193 use File::Glob ':glob';
194 @list = bsd_glob('*.[ch]');
195 $homedir = bsd_glob('~gnat', GLOB_TILDE | GLOB_ERR);
197 # an error occurred reading $homedir
200 ## override the core glob (CORE::glob() does this automatically
201 ## by default anyway, since v5.6.0)
202 use File::Glob ':globally';
203 my @sources = <*.{c,h,y}>
205 ## override the core glob, forcing case sensitivity
206 use File::Glob qw(:globally :case);
207 my @sources = <*.{c,h,y}>
209 ## override the core glob forcing case insensitivity
210 use File::Glob qw(:globally :nocase);
211 my @sources = <*.{c,h,y}>
215 File::Glob::bsd_glob() implements the FreeBSD glob(3) routine, which is
216 a superset of the POSIX glob() (described in IEEE Std 1003.2 "POSIX.2").
217 bsd_glob() takes a mandatory C<pattern> argument, and an optional
218 C<flags> argument, and returns a list of filenames matching the
219 pattern, with interpretation of the pattern modified by the C<flags>
222 Since v5.6.0, Perl's CORE::glob() is implemented in terms of bsd_glob().
223 Note that they don't share the same prototype--CORE::glob() only accepts
224 a single argument. Due to historical reasons, CORE::glob() will also
225 split its argument on whitespace, treating it as multiple patterns,
226 whereas bsd_glob() considers them as one pattern.
228 The POSIX defined flags for bsd_glob() are:
234 Force bsd_glob() to return an error when it encounters a directory it
235 cannot open or read. Ordinarily bsd_glob() continues to find matches.
239 Each pathname that is a directory that matches the pattern has a slash
244 By default, file names are assumed to be case sensitive; this flag
245 makes bsd_glob() treat case differences as not significant.
247 =item C<GLOB_NOCHECK>
249 If the pattern does not match any pathname, then bsd_glob() returns a list
250 consisting of only the pattern. If C<GLOB_QUOTE> is set, its effect
251 is present in the pattern returned.
255 By default, the pathnames are sorted in ascending ASCII order; this
256 flag prevents that sorting (speeding up bsd_glob()).
260 The FreeBSD extensions to the POSIX standard are the following flags:
266 Pre-process the string to expand C<{pat,pat,...}> strings like csh(1).
267 The pattern '{}' is left unexpanded for historical reasons (and csh(1)
268 does the same thing to ease typing of find(1) patterns).
270 =item C<GLOB_NOMAGIC>
272 Same as C<GLOB_NOCHECK> but it only returns the pattern if it does not
273 contain any of the special characters "*", "?" or "[". C<NOMAGIC> is
274 provided to simplify implementing the historic csh(1) globbing
275 behaviour and should probably not be used anywhere else.
279 Use the backslash ('\') character for quoting: every occurrence of a
280 backslash followed by a character in the pattern is replaced by that
281 character, avoiding any special interpretation of the character.
282 (But see below for exceptions on DOSISH systems).
286 Expand patterns that start with '~' to user name home directories.
290 For convenience, C<GLOB_CSH> is a synonym for
291 C<GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE>.
295 The POSIX provided C<GLOB_APPEND>, C<GLOB_DOOFFS>, and the FreeBSD
296 extensions C<GLOB_ALTDIRFUNC>, and C<GLOB_MAGCHAR> flags have not been
297 implemented in the Perl version because they involve more complex
298 interaction with the underlying C structures.
302 bsd_glob() returns a list of matching paths, possibly zero length. If an
303 error occurred, &File::Glob::GLOB_ERROR will be non-zero and C<$!> will be
304 set. &File::Glob::GLOB_ERROR is guaranteed to be zero if no error occurred,
305 or one of the following values otherwise:
309 =item C<GLOB_NOSPACE>
311 An attempt to allocate memory failed.
315 The glob was stopped because an error was encountered.
319 In the case where bsd_glob() has found some matching paths, but is
320 interrupted by an error, it will return a list of filenames B<and>
321 set &File::Glob::ERROR.
323 Note that bsd_glob() deviates from POSIX and FreeBSD glob(3) behaviour
324 by not considering C<ENOENT> and C<ENOTDIR> as errors - bsd_glob() will
325 continue processing despite those errors, unless the C<GLOB_ERR> flag is
328 Be aware that all filenames returned from File::Glob are tainted.
336 If you want to use multiple patterns, e.g. C<bsd_glob "a* b*">, you should
337 probably throw them in a set as in C<bsd_glob "{a*,b*}">. This is because
338 the argument to bsd_glob() isn't subjected to parsing by the C shell.
339 Remember that you can use a backslash to escape things.
343 On DOSISH systems, backslash is a valid directory separator character.
344 In this case, use of backslash as a quoting character (via GLOB_QUOTE)
345 interferes with the use of backslash as a directory separator. The
346 best (simplest, most portable) solution is to use forward slashes for
347 directory separators, and backslashes for quoting. However, this does
348 not match "normal practice" on these systems. As a concession to user
349 expectation, therefore, backslashes (under GLOB_QUOTE) only quote the
350 glob metacharacters '[', ']', '{', '}', '-', '~', and backslash itself.
351 All other backslashes are passed through unchanged.
355 Win32 users should use the real slash. If you really want to use
356 backslashes, consider using Sarathy's File::DosGlob, which comes with
357 the standard Perl distribution.
363 The Perl interface was written by Nathan Torkington E<lt>gnat@frii.comE<gt>,
364 and is released under the artistic license. Further modifications were
365 made by Greg Bacon E<lt>gbacon@cs.uah.eduE<gt> and Gurusamy Sarathy
366 E<lt>gsar@activestate.comE<gt>. The C glob code has the
369 Copyright (c) 1989, 1993 The Regents of the University of California.
372 This code is derived from software contributed to Berkeley by
375 Redistribution and use in source and binary forms, with or without
376 modification, are permitted provided that the following conditions
379 1. Redistributions of source code must retain the above copyright
380 notice, this list of conditions and the following disclaimer.
381 2. 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.
384 3. Neither the name of the University nor the names of its contributors
385 may be used to endorse or promote products derived from this software
386 without specific prior written permission.
388 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
389 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
390 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
391 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
392 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
393 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
394 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
395 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
396 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
397 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF