1File::Glob(3pm) Perl Programmers Reference Guide File::Glob(3pm)
2
3
4
6 File::Glob - Perl extension for BSD glob routine
7
9 use File::Glob ':bsd_glob';
10
11 @list = bsd_glob('*.[ch]');
12 $homedir = bsd_glob('~gnat', GLOB_TILDE | GLOB_ERR);
13
14 if (GLOB_ERROR) {
15 # an error occurred reading $homedir
16 }
17
18 ## override the core glob (CORE::glob() does this automatically
19 ## by default anyway, since v5.6.0)
20 use File::Glob ':globally';
21 my @sources = <*.{c,h,y}>;
22
23 ## override the core glob, forcing case sensitivity
24 use File::Glob qw(:globally :case);
25 my @sources = <*.{c,h,y}>;
26
27 ## override the core glob forcing case insensitivity
28 use File::Glob qw(:globally :nocase);
29 my @sources = <*.{c,h,y}>;
30
31 ## glob on all files in home directory
32 use File::Glob ':globally';
33 my @sources = <~gnat/*>;
34
36 The glob angle-bracket operator "<>" is a pathname generator that
37 implements the rules for file name pattern matching used by Unix-like
38 shells such as the Bourne shell or C shell.
39
40 File::Glob::bsd_glob() implements the FreeBSD glob(3) routine, which is
41 a superset of the POSIX glob() (described in IEEE Std 1003.2
42 "POSIX.2"). bsd_glob() takes a mandatory "pattern" argument, and an
43 optional "flags" argument, and returns a list of filenames matching the
44 pattern, with interpretation of the pattern modified by the "flags"
45 variable.
46
47 Since v5.6.0, Perl's CORE::glob() is implemented in terms of
48 bsd_glob(). Note that they don't share the same
49 prototype--CORE::glob() only accepts a single argument. Due to
50 historical reasons, CORE::glob() will also split its argument on
51 whitespace, treating it as multiple patterns, whereas bsd_glob()
52 considers them as one pattern. But see ":bsd_glob" under "EXPORTS",
53 below.
54
55 META CHARACTERS
56 \ Quote the next metacharacter
57 [] Character class
58 {} Multiple pattern
59 * Match any string of characters
60 ? Match any single character
61 ~ User name home directory
62
63 The metanotation "a{b,c,d}e" is a shorthand for "abe ace ade". Left to
64 right order is preserved, with results of matches being sorted
65 separately at a low level to preserve this order. As a special case
66 "{", "}", and "{}" are passed undisturbed.
67
68 EXPORTS
69 See also the "POSIX FLAGS" below, which can be exported individually.
70
71 ":bsd_glob"
72
73 The ":bsd_glob" export tag exports bsd_glob() and the constants listed
74 below. It also overrides glob() in the calling package with one that
75 behaves like bsd_glob() with regard to spaces (the space is treated as
76 part of a file name), but supports iteration in scalar context; i.e.,
77 it preserves the core function's feature of returning the next item
78 each time it is called.
79
80 ":glob"
81
82 The ":glob" tag, now discouraged, is the old version of ":bsd_glob".
83 It exports the same constants and functions, but its glob() override
84 does not support iteration; it returns the last file name in scalar
85 context. That means this will loop forever:
86
87 use File::Glob ':glob';
88 while (my $file = <* copy.txt>) {
89 ...
90 }
91
92 "bsd_glob"
93
94 This function, which is included in the two export tags listed above,
95 takes one or two arguments. The first is the glob pattern. The second
96 is a set of flags ORed together. The available flags are listed below
97 under "POSIX FLAGS". If the second argument is omitted, "GLOB_CSH" (or
98 "GLOB_CSH|GLOB_NOCASE" on VMS and DOSish systems) is used by default.
99
100 ":nocase" and ":case"
101
102 These two export tags globally modify the default flags that bsd_glob()
103 and, except on VMS, Perl's built-in "glob" operator use. "GLOB_NOCASE"
104 is turned on or off, respectively.
105
106 "csh_glob"
107
108 The csh_glob() function can also be exported, but you should not use it
109 directly unless you really know what you are doing. It splits the
110 pattern into words and feeds each one to bsd_glob(). Perl's own glob()
111 function uses this internally.
112
113 POSIX FLAGS
114 The POSIX defined flags for bsd_glob() are:
115
116 "GLOB_ERR"
117 Force bsd_glob() to return an error when it encounters a directory
118 it cannot open or read. Ordinarily bsd_glob() continues to find
119 matches.
120
121 "GLOB_LIMIT"
122 Make bsd_glob() return an error (GLOB_NOSPACE) when the pattern
123 expands to a size bigger than the system constant "ARG_MAX"
124 (usually found in limits.h). If your system does not define this
125 constant, bsd_glob() uses "sysconf(_SC_ARG_MAX)" or
126 "_POSIX_ARG_MAX" where available (in that order). You can inspect
127 these values using the standard "POSIX" extension.
128
129 "GLOB_MARK"
130 Each pathname that is a directory that matches the pattern has a
131 slash appended.
132
133 "GLOB_NOCASE"
134 By default, file names are assumed to be case sensitive; this flag
135 makes bsd_glob() treat case differences as not significant.
136
137 "GLOB_NOCHECK"
138 If the pattern does not match any pathname, then bsd_glob() returns
139 a list consisting of only the pattern. If "GLOB_QUOTE" is set, its
140 effect is present in the pattern returned.
141
142 "GLOB_NOSORT"
143 By default, the pathnames are sorted in ascending ASCII order; this
144 flag prevents that sorting (speeding up bsd_glob()).
145
146 The FreeBSD extensions to the POSIX standard are the following flags:
147
148 "GLOB_BRACE"
149 Pre-process the string to expand "{pat,pat,...}" strings like
150 csh(1). The pattern '{}' is left unexpanded for historical reasons
151 (and csh(1) does the same thing to ease typing of find(1)
152 patterns).
153
154 "GLOB_NOMAGIC"
155 Same as "GLOB_NOCHECK" but it only returns the pattern if it does
156 not contain any of the special characters "*", "?" or "[".
157 "NOMAGIC" is provided to simplify implementing the historic csh(1)
158 globbing behaviour and should probably not be used anywhere else.
159
160 "GLOB_QUOTE"
161 Use the backslash ('\') character for quoting: every occurrence of
162 a backslash followed by a character in the pattern is replaced by
163 that character, avoiding any special interpretation of the
164 character. (But see below for exceptions on DOSISH systems).
165
166 "GLOB_TILDE"
167 Expand patterns that start with '~' to user name home directories.
168
169 "GLOB_CSH"
170 For convenience, "GLOB_CSH" is a synonym for "GLOB_BRACE |
171 GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE | GLOB_ALPHASORT".
172
173 The POSIX provided "GLOB_APPEND", "GLOB_DOOFFS", and the FreeBSD
174 extensions "GLOB_ALTDIRFUNC", and "GLOB_MAGCHAR" flags have not been
175 implemented in the Perl version because they involve more complex
176 interaction with the underlying C structures.
177
178 The following flag has been added in the Perl implementation for csh
179 compatibility:
180
181 "GLOB_ALPHASORT"
182 If "GLOB_NOSORT" is not in effect, sort filenames is alphabetical
183 order (case does not matter) rather than in ASCII order.
184
186 bsd_glob() returns a list of matching paths, possibly zero length. If
187 an error occurred, &File::Glob::GLOB_ERROR will be non-zero and $! will
188 be set. &File::Glob::GLOB_ERROR is guaranteed to be zero if no error
189 occurred, or one of the following values otherwise:
190
191 "GLOB_NOSPACE"
192 An attempt to allocate memory failed.
193
194 "GLOB_ABEND"
195 The glob was stopped because an error was encountered.
196
197 In the case where bsd_glob() has found some matching paths, but is
198 interrupted by an error, it will return a list of filenames and set
199 &File::Glob::ERROR.
200
201 Note that bsd_glob() deviates from POSIX and FreeBSD glob(3) behaviour
202 by not considering "ENOENT" and "ENOTDIR" as errors - bsd_glob() will
203 continue processing despite those errors, unless the "GLOB_ERR" flag is
204 set.
205
206 Be aware that all filenames returned from File::Glob are tainted.
207
209 · If you want to use multiple patterns, e.g. "bsd_glob("a* b*")", you
210 should probably throw them in a set as in "bsd_glob("{a*,b*}")".
211 This is because the argument to bsd_glob() isn't subjected to
212 parsing by the C shell. Remember that you can use a backslash to
213 escape things.
214
215 · On DOSISH systems, backslash is a valid directory separator
216 character. In this case, use of backslash as a quoting character
217 (via GLOB_QUOTE) interferes with the use of backslash as a
218 directory separator. The best (simplest, most portable) solution
219 is to use forward slashes for directory separators, and backslashes
220 for quoting. However, this does not match "normal practice" on
221 these systems. As a concession to user expectation, therefore,
222 backslashes (under GLOB_QUOTE) only quote the glob metacharacters
223 '[', ']', '{', '}', '-', '~', and backslash itself. All other
224 backslashes are passed through unchanged.
225
226 · Win32 users should use the real slash. If you really want to use
227 backslashes, consider using Sarathy's File::DosGlob, which comes
228 with the standard Perl distribution.
229
231 "glob" in perlfunc, glob(3)
232
234 The Perl interface was written by Nathan Torkington <gnat@frii.com>,
235 and is released under the artistic license. Further modifications were
236 made by Greg Bacon <gbacon@cs.uah.edu>, Gurusamy Sarathy
237 <gsar@activestate.com>, and Thomas Wegner <wegner_thomas@yahoo.com>.
238 The C glob code has the following copyright:
239
240 Copyright (c) 1989, 1993 The Regents of the University of California.
241 All rights reserved.
242
243 This code is derived from software contributed to Berkeley by
244 Guido van Rossum.
245
246 Redistribution and use in source and binary forms, with or without
247 modification, are permitted provided that the following conditions
248 are met:
249
250 1. Redistributions of source code must retain the above copyright
251 notice, this list of conditions and the following disclaimer.
252 2. Redistributions in binary form must reproduce the above copyright
253 notice, this list of conditions and the following disclaimer in the
254 documentation and/or other materials provided with the distribution.
255 3. Neither the name of the University nor the names of its contributors
256 may be used to endorse or promote products derived from this software
257 without specific prior written permission.
258
259 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
260 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
262 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
263 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
265 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
266 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
267 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269 SUCH DAMAGE.
270
271
272
273perl v5.16.3 2013-03-04 File::Glob(3pm)