1package File::Glob; 2 3use strict; 4our($VERSION, @ISA, @EXPORT_OK, @EXPORT_FAIL, %EXPORT_TAGS, $DEFAULT_FLAGS); 5 6require XSLoader; 7 8@ISA = qw(Exporter); 9 10# NOTE: The glob() export is only here for compatibility with 5.6.0. 11# csh_glob() should not be used directly, unless you know what you're doing. 12 13%EXPORT_TAGS = ( 14 'glob' => [ qw( 15 GLOB_ABEND 16 GLOB_ALPHASORT 17 GLOB_ALTDIRFUNC 18 GLOB_BRACE 19 GLOB_CSH 20 GLOB_ERR 21 GLOB_ERROR 22 GLOB_LIMIT 23 GLOB_MARK 24 GLOB_NOCASE 25 GLOB_NOCHECK 26 GLOB_NOMAGIC 27 GLOB_NOSORT 28 GLOB_NOSPACE 29 GLOB_QUOTE 30 GLOB_TILDE 31 bsd_glob 32 ) ], 33); 34$EXPORT_TAGS{bsd_glob} = [@{$EXPORT_TAGS{glob}}]; 35 36@EXPORT_OK = (@{$EXPORT_TAGS{'glob'}}, 'csh_glob'); 37 38$VERSION = '1.33'; 39 40sub import { 41 require Exporter; 42 local $Exporter::ExportLevel = $Exporter::ExportLevel + 1; 43 Exporter::import(grep { 44 my $passthrough; 45 if ($_ eq ':case') { 46 $DEFAULT_FLAGS &= ~GLOB_NOCASE() 47 } 48 elsif ($_ eq ':nocase') { 49 $DEFAULT_FLAGS |= GLOB_NOCASE(); 50 } 51 elsif ($_ eq ':globally') { 52 no warnings 'redefine'; 53 *CORE::GLOBAL::glob = \&File::Glob::csh_glob; 54 } 55 elsif ($_ eq ':bsd_glob') { 56 no strict; *{caller."::glob"} = \&bsd_glob_override; 57 $passthrough = 1; 58 } 59 else { 60 $passthrough = 1; 61 } 62 $passthrough; 63 } @_); 64} 65 66XSLoader::load(); 67 68$DEFAULT_FLAGS = GLOB_CSH(); 69if ($^O =~ /^(?:MSWin32|VMS|os2|dos|riscos)$/) { 70 $DEFAULT_FLAGS |= GLOB_NOCASE(); 71} 72 731; 74__END__ 75 76=head1 NAME 77 78File::Glob - Perl extension for BSD glob routine 79 80=head1 SYNOPSIS 81 82 use File::Glob ':bsd_glob'; 83 84 @list = bsd_glob('*.[ch]'); 85 $homedir = bsd_glob('~gnat', GLOB_TILDE | GLOB_ERR); 86 87 if (GLOB_ERROR) { 88 # an error occurred reading $homedir 89 } 90 91 ## override the core glob (CORE::glob() does this automatically 92 ## by default anyway, since v5.6.0) 93 use File::Glob ':globally'; 94 my @sources = <*.{c,h,y}>; 95 96 ## override the core glob, forcing case sensitivity 97 use File::Glob qw(:globally :case); 98 my @sources = <*.{c,h,y}>; 99 100 ## override the core glob forcing case insensitivity 101 use File::Glob qw(:globally :nocase); 102 my @sources = <*.{c,h,y}>; 103 104 ## glob on all files in home directory 105 use File::Glob ':globally'; 106 my @sources = <~gnat/*>; 107 108=head1 DESCRIPTION 109 110The glob angle-bracket operator C<< <> >> is a pathname generator that 111implements the rules for file name pattern matching used by Unix-like shells 112such as the Bourne shell or C shell. 113 114File::Glob::bsd_glob() implements the FreeBSD glob(3) routine, which is 115a superset of the POSIX glob() (described in IEEE Std 1003.2 "POSIX.2"). 116bsd_glob() takes a mandatory C<pattern> argument, and an optional 117C<flags> argument, and returns a list of filenames matching the 118pattern, with interpretation of the pattern modified by the C<flags> 119variable. 120 121Since v5.6.0, Perl's CORE::glob() is implemented in terms of bsd_glob(). 122Note that they don't share the same prototype--CORE::glob() only accepts 123a single argument. Due to historical reasons, CORE::glob() will also 124split its argument on whitespace, treating it as multiple patterns, 125whereas bsd_glob() considers them as one pattern. But see C<:bsd_glob> 126under L</EXPORTS>, below. 127 128=head2 META CHARACTERS 129 130 \ Quote the next metacharacter 131 [] Character class 132 {} Multiple pattern 133 * Match any string of characters 134 ? Match any single character 135 ~ User name home directory 136 137The metanotation C<a{b,c,d}e> is a shorthand for C<abe ace ade>. Left to 138right order is preserved, with results of matches being sorted separately 139at a low level to preserve this order. As a special case C<{>, C<}>, and 140C<{}> are passed undisturbed. 141 142=head2 EXPORTS 143 144See also the L</POSIX FLAGS> below, which can be exported individually. 145 146=head3 C<:bsd_glob> 147 148The C<:bsd_glob> export tag exports bsd_glob() and the constants listed 149below. It also overrides glob() in the calling package with one that 150behaves like bsd_glob() with regard to spaces (the space is treated as part 151of a file name), but supports iteration in scalar context; i.e., it 152preserves the core function's feature of returning the next item each time 153it is called. 154 155=head3 C<:glob> 156 157The C<:glob> tag, now discouraged, is the old version of C<:bsd_glob>. It 158exports the same constants and functions, but its glob() override does not 159support iteration; it returns the last file name in scalar context. That 160means this will loop forever: 161 162 use File::Glob ':glob'; 163 while (my $file = <* copy.txt>) { 164 ... 165 } 166 167=head3 C<bsd_glob> 168 169This function, which is included in the two export tags listed above, 170takes one or two arguments. The first is the glob pattern. The 171second, if given, is a set of flags ORed together. The available 172flags and the default set of flags are listed below under L</POSIX FLAGS>. 173 174Remember that to use the named constants for flags you must import 175them, for example with C<:bsd_glob> described above. If not imported, 176and C<use strict> is not in effect, then the constants will be 177treated as bareword strings, which won't do what you what. 178 179 180=head3 C<:nocase> and C<:case> 181 182These two export tags globally modify the default flags that bsd_glob() 183and, except on VMS, Perl's built-in C<glob> operator use. C<GLOB_NOCASE> 184is turned on or off, respectively. 185 186=head3 C<csh_glob> 187 188The csh_glob() function can also be exported, but you should not use it 189directly unless you really know what you are doing. It splits the pattern 190into words and feeds each one to bsd_glob(). Perl's own glob() function 191uses this internally. 192 193=head2 POSIX FLAGS 194 195If no flags argument is give then C<GLOB_CSH> is set, and on VMS and 196Windows systems, C<GLOB_NOCASE> too. Otherwise the flags to use are 197determined solely by the flags argument. The POSIX defined flags are: 198 199=over 4 200 201=item C<GLOB_ERR> 202 203Force bsd_glob() to return an error when it encounters a directory it 204cannot open or read. Ordinarily bsd_glob() continues to find matches. 205 206=item C<GLOB_LIMIT> 207 208Make bsd_glob() return an error (GLOB_NOSPACE) when the pattern expands 209to a size bigger than the system constant C<ARG_MAX> (usually found in 210limits.h). If your system does not define this constant, bsd_glob() uses 211C<sysconf(_SC_ARG_MAX)> or C<_POSIX_ARG_MAX> where available (in that 212order). You can inspect these values using the standard C<POSIX> 213extension. 214 215=item C<GLOB_MARK> 216 217Each pathname that is a directory that matches the pattern has a slash 218appended. 219 220=item C<GLOB_NOCASE> 221 222By default, file names are assumed to be case sensitive; this flag 223makes bsd_glob() treat case differences as not significant. 224 225=item C<GLOB_NOCHECK> 226 227If the pattern does not match any pathname, then bsd_glob() returns a list 228consisting of only the pattern. If C<GLOB_QUOTE> is set, its effect 229is present in the pattern returned. 230 231=item C<GLOB_NOSORT> 232 233By default, the pathnames are sorted in ascending ASCII order; this 234flag prevents that sorting (speeding up bsd_glob()). 235 236=back 237 238The FreeBSD extensions to the POSIX standard are the following flags: 239 240=over 4 241 242=item C<GLOB_BRACE> 243 244Pre-process the string to expand C<{pat,pat,...}> strings like csh(1). 245The pattern '{}' is left unexpanded for historical reasons (and csh(1) 246does the same thing to ease typing of find(1) patterns). 247 248=item C<GLOB_NOMAGIC> 249 250Same as C<GLOB_NOCHECK> but it only returns the pattern if it does not 251contain any of the special characters "*", "?" or "[". C<NOMAGIC> is 252provided to simplify implementing the historic csh(1) globbing 253behaviour and should probably not be used anywhere else. 254 255=item C<GLOB_QUOTE> 256 257Use the backslash ('\') character for quoting: every occurrence of a 258backslash followed by a character in the pattern is replaced by that 259character, avoiding any special interpretation of the character. 260(But see below for exceptions on DOSISH systems). 261 262=item C<GLOB_TILDE> 263 264Expand patterns that start with '~' to user name home directories. 265 266=item C<GLOB_CSH> 267 268For convenience, C<GLOB_CSH> is a synonym for 269C<GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE | GLOB_ALPHASORT>. 270 271=back 272 273The POSIX provided C<GLOB_APPEND>, C<GLOB_DOOFFS>, and the FreeBSD 274extensions C<GLOB_ALTDIRFUNC>, and C<GLOB_MAGCHAR> flags have not been 275implemented in the Perl version because they involve more complex 276interaction with the underlying C structures. 277 278The following flag has been added in the Perl implementation for 279csh compatibility: 280 281=over 4 282 283=item C<GLOB_ALPHASORT> 284 285If C<GLOB_NOSORT> is not in effect, sort filenames is alphabetical 286order (case does not matter) rather than in ASCII order. 287 288=back 289 290=head1 DIAGNOSTICS 291 292bsd_glob() returns a list of matching paths, possibly zero length. If an 293error occurred, &File::Glob::GLOB_ERROR will be non-zero and C<$!> will be 294set. &File::Glob::GLOB_ERROR is guaranteed to be zero if no error occurred, 295or one of the following values otherwise: 296 297=over 4 298 299=item C<GLOB_NOSPACE> 300 301An attempt to allocate memory failed. 302 303=item C<GLOB_ABEND> 304 305The glob was stopped because an error was encountered. 306 307=back 308 309In the case where bsd_glob() has found some matching paths, but is 310interrupted by an error, it will return a list of filenames B<and> 311set &File::Glob::ERROR. 312 313Note that bsd_glob() deviates from POSIX and FreeBSD glob(3) behaviour 314by not considering C<ENOENT> and C<ENOTDIR> as errors - bsd_glob() will 315continue processing despite those errors, unless the C<GLOB_ERR> flag is 316set. 317 318Be aware that all filenames returned from File::Glob are tainted. 319 320=head1 NOTES 321 322=over 4 323 324=item * 325 326If you want to use multiple patterns, e.g. C<bsd_glob("a* b*")>, you should 327probably throw them in a set as in C<bsd_glob("{a*,b*}")>. This is because 328the argument to bsd_glob() isn't subjected to parsing by the C shell. 329Remember that you can use a backslash to escape things. 330 331=item * 332 333On DOSISH systems, backslash is a valid directory separator character. 334In this case, use of backslash as a quoting character (via GLOB_QUOTE) 335interferes with the use of backslash as a directory separator. The 336best (simplest, most portable) solution is to use forward slashes for 337directory separators, and backslashes for quoting. However, this does 338not match "normal practice" on these systems. As a concession to user 339expectation, therefore, backslashes (under GLOB_QUOTE) only quote the 340glob metacharacters '[', ']', '{', '}', '-', '~', and backslash itself. 341All other backslashes are passed through unchanged. 342 343=item * 344 345Win32 users should use the real slash. If you really want to use 346backslashes, consider using Sarathy's File::DosGlob, which comes with 347the standard Perl distribution. 348 349=back 350 351=head1 SEE ALSO 352 353L<perlfunc/glob>, glob(3) 354 355=head1 AUTHOR 356 357The Perl interface was written by Nathan Torkington E<lt>gnat@frii.comE<gt>, 358and is released under the artistic license. Further modifications were 359made by Greg Bacon E<lt>gbacon@cs.uah.eduE<gt>, Gurusamy Sarathy 360E<lt>gsar@activestate.comE<gt>, and Thomas Wegner 361E<lt>wegner_thomas@yahoo.comE<gt>. The C glob code has the 362following copyright: 363 364Copyright (c) 1989, 1993 The Regents of the University of California. 365All rights reserved. 366 367This code is derived from software contributed to Berkeley by 368Guido van Rossum. 369 370Redistribution and use in source and binary forms, with or without 371modification, are permitted provided that the following conditions 372are met: 373 374=over 4 375 376=item 1. 377 378Redistributions of source code must retain the above copyright 379notice, this list of conditions and the following disclaimer. 380 381=item 2. 382 383Redistributions in binary form must reproduce the above copyright 384notice, this list of conditions and the following disclaimer in the 385documentation and/or other materials provided with the distribution. 386 387=item 3. 388 389Neither the name of the University nor the names of its contributors 390may be used to endorse or promote products derived from this software 391without specific prior written permission. 392 393=back 394 395THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND 396ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 397IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 398ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 399FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 400DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 401OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 402HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 403LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 404OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 405SUCH DAMAGE. 406 407=cut 408