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