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 ':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 proto‐
49 type--CORE::glob() only accepts a single argument. Due to historical
50 reasons, CORE::glob() will also split its argument on whitespace,
51 treating it as multiple patterns, whereas bsd_glob() considers them as
52 one pattern.
53
54 META CHARACTERS
55
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 sepa‐
65 rately at a low level to preserve this order. As a special case "{",
66 "}", and "{}" are passed undisturbed.
67
68 POSIX FLAGS
69
70 The POSIX defined flags for bsd_glob() are:
71
72 "GLOB_ERR"
73 Force bsd_glob() to return an error when it encounters a directory
74 it cannot open or read. Ordinarily bsd_glob() continues to find
75 matches.
76
77 "GLOB_LIMIT"
78 Make bsd_glob() return an error (GLOB_NOSPACE) when the pattern
79 expands to a size bigger than the system constant "ARG_MAX" (usu‐
80 ally found in limits.h). If your system does not define this con‐
81 stant, bsd_glob() uses "sysconf(_SC_ARG_MAX)" or "_POSIX_ARG_MAX"
82 where available (in that order). You can inspect these values
83 using the standard "POSIX" extension.
84
85 "GLOB_MARK"
86 Each pathname that is a directory that matches the pattern has a
87 slash appended.
88
89 "GLOB_NOCASE"
90 By default, file names are assumed to be case sensitive; this flag
91 makes bsd_glob() treat case differences as not significant.
92
93 "GLOB_NOCHECK"
94 If the pattern does not match any pathname, then bsd_glob() returns
95 a list consisting of only the pattern. If "GLOB_QUOTE" is set, its
96 effect is present in the pattern returned.
97
98 "GLOB_NOSORT"
99 By default, the pathnames are sorted in ascending ASCII order; this
100 flag prevents that sorting (speeding up bsd_glob()).
101
102 The FreeBSD extensions to the POSIX standard are the following flags:
103
104 "GLOB_BRACE"
105 Pre-process the string to expand "{pat,pat,...}" strings like
106 csh(1). The pattern '{}' is left unexpanded for historical reasons
107 (and csh(1) does the same thing to ease typing of find(1) pat‐
108 terns).
109
110 "GLOB_NOMAGIC"
111 Same as "GLOB_NOCHECK" but it only returns the pattern if it does
112 not contain any of the special characters "*", "?" or "[".
113 "NOMAGIC" is provided to simplify implementing the historic csh(1)
114 globbing behaviour and should probably not be used anywhere else.
115
116 "GLOB_QUOTE"
117 Use the backslash ('\') character for quoting: every occurrence of
118 a backslash followed by a character in the pattern is replaced by
119 that character, avoiding any special interpretation of the charac‐
120 ter. (But see below for exceptions on DOSISH systems).
121
122 "GLOB_TILDE"
123 Expand patterns that start with '~' to user name home directories.
124
125 "GLOB_CSH"
126 For convenience, "GLOB_CSH" is a synonym for "GLOB_BRACE ⎪
127 GLOB_NOMAGIC ⎪ GLOB_QUOTE ⎪ GLOB_TILDE ⎪ GLOB_ALPHASORT".
128
129 The POSIX provided "GLOB_APPEND", "GLOB_DOOFFS", and the FreeBSD exten‐
130 sions "GLOB_ALTDIRFUNC", and "GLOB_MAGCHAR" flags have not been imple‐
131 mented in the Perl version because they involve more complex interac‐
132 tion with the underlying C structures.
133
134 The following flag has been added in the Perl implementation for csh
135 compatibility:
136
137 "GLOB_ALPHASORT"
138 If "GLOB_NOSORT" is not in effect, sort filenames is alphabetical
139 order (case does not matter) rather than in ASCII order.
140
142 bsd_glob() returns a list of matching paths, possibly zero length. If
143 an error occurred, &File::Glob::GLOB_ERROR will be non-zero and $! will
144 be set. &File::Glob::GLOB_ERROR is guaranteed to be zero if no error
145 occurred, or one of the following values otherwise:
146
147 "GLOB_NOSPACE"
148 An attempt to allocate memory failed.
149
150 "GLOB_ABEND"
151 The glob was stopped because an error was encountered.
152
153 In the case where bsd_glob() has found some matching paths, but is
154 interrupted by an error, it will return a list of filenames and set
155 &File::Glob::ERROR.
156
157 Note that bsd_glob() deviates from POSIX and FreeBSD glob(3) behaviour
158 by not considering "ENOENT" and "ENOTDIR" as errors - bsd_glob() will
159 continue processing despite those errors, unless the "GLOB_ERR" flag is
160 set.
161
162 Be aware that all filenames returned from File::Glob are tainted.
163
165 · If you want to use multiple patterns, e.g. "bsd_glob("a* b*")", you
166 should probably throw them in a set as in "bsd_glob("{a*,b*}")".
167 This is because the argument to bsd_glob() isn't subjected to pars‐
168 ing by the C shell. Remember that you can use a backslash to
169 escape things.
170
171 · On DOSISH systems, backslash is a valid directory separator charac‐
172 ter. In this case, use of backslash as a quoting character (via
173 GLOB_QUOTE) interferes with the use of backslash as a directory
174 separator. The best (simplest, most portable) solution is to use
175 forward slashes for directory separators, and backslashes for quot‐
176 ing. However, this does not match "normal practice" on these sys‐
177 tems. As a concession to user expectation, therefore, backslashes
178 (under GLOB_QUOTE) only quote the glob metacharacters '[', ']',
179 '{', '}', '-', '~', and backslash itself. All other backslashes
180 are passed through unchanged.
181
182 · Win32 users should use the real slash. If you really want to use
183 backslashes, consider using Sarathy's File::DosGlob, which comes
184 with the standard Perl distribution.
185
186 · Mac OS (Classic) users should note a few differences. Since Mac OS
187 is not Unix, when the glob code encounters a tilde glob (e.g.
188 ~user) and the "GLOB_TILDE" flag is used, it simply returns that
189 pattern without doing any expansion.
190
191 Glob on Mac OS is case-insensitive by default (if you don't use any
192 flags). If you specify any flags at all and still want glob to be
193 case-insensitive, you must include "GLOB_NOCASE" in the flags.
194
195 The path separator is ':' (aka colon), not '/' (aka slash). Mac OS
196 users should be careful about specifying relative pathnames. While
197 a full path always begins with a volume name, a relative pathname
198 should always begin with a ':'. If specifying a volume name only,
199 a trailing ':' is required.
200
201 The specification of pathnames in glob patterns adheres to the
202 usual Mac OS conventions: The path separator is a colon ':', not a
203 slash '/'. A full path always begins with a volume name. A relative
204 pathname on Mac OS must always begin with a ':', except when speci‐
205 fying a file or directory name in the current working directory,
206 where the leading colon is optional. If specifying a volume name
207 only, a trailing ':' is required. Due to these rules, a glob like
208 <*:> will find all mounted volumes, while a glob like <*> or <:*>
209 will find all files and directories in the current directory.
210
211 Note that updirs in the glob pattern are resolved before the match‐
212 ing begins, i.e. a pattern like "*HD:t?p::a*" will be matched as
213 "*HD:a*". Note also, that a single trailing ':' in the pattern is
214 ignored (unless it's a volume name pattern like "*HD:"), i.e. a
215 glob like <:*:> will find both directories and files (and not, as
216 one might expect, only directories). You can, however, use the
217 "GLOB_MARK" flag to distinguish (without a file test) directory
218 names from file names.
219
220 If the "GLOB_MARK" flag is set, all directory paths will have a ':'
221 appended. Since a directory like 'lib:' is not a valid relative
222 path on Mac OS, both a leading and a trailing colon will be added,
223 when the directory name in question doesn't contain any colons
224 (e.g. 'lib' becomes ':lib:').
225
227 "glob" in perlfunc, glob(3)
228
230 The Perl interface was written by Nathan Torkington <gnat@frii.com>,
231 and is released under the artistic license. Further modifications were
232 made by Greg Bacon <gbacon@cs.uah.edu>, Gurusamy Sarathy <gsar@actives‐
233 tate.com>, and Thomas Wegner <wegner_thomas@yahoo.com>. The C glob
234 code has the following copyright:
235
236 Copyright (c) 1989, 1993 The Regents of the University of California.
237 All rights reserved.
238
239 This code is derived from software contributed to Berkeley by
240 Guido van Rossum.
241
242 Redistribution and use in source and binary forms, with or without
243 modification, are permitted provided that the following conditions
244 are met:
245
246 1. Redistributions of source code must retain the above copyright
247 notice, this list of conditions and the following disclaimer.
248 2. Redistributions in binary form must reproduce the above copyright
249 notice, this list of conditions and the following disclaimer in the
250 documentation and/or other materials provided with the distribution.
251 3. Neither the name of the University nor the names of its contributors
252 may be used to endorse or promote products derived from this software
253 without specific prior written permission.
254
255 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
256 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
257 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
263 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
265 SUCH DAMAGE.
266
267
268
269perl v5.8.8 2001-09-21 File::Glob(3pm)