Debian Codegraph @

README

raw
1 NAME
2 File::Wildcard - Enhanced glob processing
3
4 SYNOPSIS
5 use File::Wildcard;
6 my $foo = File::Wildcard->new(path => "/home/me///core");
7 while (my $file = $foo->next) {
8 unlink $file;
9 }
10
11 DESCRIPTION
12 When looking at how various operating systems do filename wildcard
13 expansion (globbing), VMS has a nice syntax which allows expansion and
14 searching of whole directory trees. It would be nice if other operating
15 systems had something like this built in. The best Unix can manage is
16 through the utility program "find".
17
18 This module provides this facility to Perl. Whereas native VMS syntax
19 uses the ellipsis "...", this will not fit in with POSIX filenames, as
20 ... is a valid (though somewhat strange) filename. Instead, the
21 construct "///" is used as this cannot syntactically be part of a
22 filename, as you do not get three concurrent filename separators with
23 nothing between (three slashes are used to avoid confusion with
24 //node/path/name syntax).
25
26 You don't have to use this syntax, as you can do the splitting yourself
27 and pass in an arrayref as your path.
28
29 The module also forms a regular expression for the whole of the wildcard
30 string, and binds a series of back references ($1, $2 etc.) which are
31 available to construct new filenames.
32
33 new
34 "File::Wildcard-"new( $wildcard, [,option => value,...]);>
35
36 my $foo = File::Wildcard->new( path => "/home/me///core");
37 my $srcfnd = File::Wildcard->new( path => "src///*.cpp",
38 match => qr(^src/(.*?)\.cpp$),
39 derive => ['src/$1.o','src/$1.hpp']);
40
41 This is the constructor for File::Wildcard objects. At a simple level,
42 pass a single wildcard string as a path.
43
44 For more complicated operations, you can supply your own match regexp,
45 or use the derive option to specify regular expression captures to form
46 the basis of other filenames that are constructed for you.
47
48 The $srcfnd example gives you object files and header files
49 corresponding to C++ source files.
50
51 Here are the options that are available:
52
53 "path"
54 This is the input parameter that specifies the range of files that
55 will be looked at. This is a glob spec which can also contain the
56 ellipsis '///' (it could contain more than one ellipsis, but the
57 benefit of this is questionable, and multiple ellipsi would cause a
58 performance hit).
59
60 Note that the path can be relative or absolute. new will do the
61 right thing, working out that a path starting with '/' is absolute.
62 In order to recurse from the current directory downwards, specify
63 './//foo'.
64
65 As an alternative, you can supply an arrayref with the path
66 constituents already split. If you do this, you need to tell new if
67 the path is absolute. Include an empty string for an ellipsis. For
68 example:
69
70 'foo///bar/*.c' is equivalent to ['foo','','bar','*.c']
71
72 You can also construct a File::Wildcard without a path. A call to
73 next will return undef, but paths can be added using the append and
74 prepend methods.
75
76 "absolute"
77 This is ignored unless you are using a pre split path. If you are
78 passing a string as the path, new will work out whether the path is
79 absolute or relative. Pass a true value for absolute paths.
80
81 If your original filespec started with '/' before you split it,
82 specify absolute => 1. absolute is not required for Windows if the
83 path contains a drive specification, e.g. C:/foo/bar.
84
85 "case_insensitive"
86 By default, the module will use Filesys::Type to determine whether
87 the file system of your wildcard is defined. This is an optional
88 module (see Module::Optional), and File::Wildcard will guess at case
89 sensitivity based on your operating system. This will not always be
90 correct, as the file system might be VFAT mounted on Linux or ODS-5
91 on VMS.
92
93 Specifying the option "case_insensitive" explicitly forces this
94 behaviour on the wildcard.
95
96 Note that File::Wildcard will use the file system of the current
97 working directory if the path is not absolute. If the path is
98 absolute, you should specify the case_sensitivity option explicitly.
99
100 "exclude"
101 You can provide a regexp to apply to any generated paths, which will
102 cause any matching paths not to be processed. If the root of a
103 directory tree matches, no processing is done on the entire tree.
104
105 This option can be useful for excluding version control
106 repositories, e.g.
107
108 exclude => qr/.svn/
109
110 "match"
111 Optional. If you do not specify a regexp, you get all the files that
112 match the glob; in addition, new will set up a regexp for you, to
113 provide a capture for each wildcard used in the path.
114
115 If you do provide a match parameter, this will be used instead, and
116 will filter the results.
117
118 "derive"
119 Supply an arrayref with a list of derived filenames, which will be
120 constructed for each matching file. This causes next to return an
121 arrayref instead of a scalar.
122
123 "follow"
124 If given a true value indicates that symbolic links are to be
125 followed. Otherwise, the symbolic link target itself is presented,
126 but the ellipsis will not traverse the link.
127
128 This module detects a looping symlink that points to a directory
129 higher up, and will only present the tree once.
130
131 "ellipsis_order"
132 This can take one of the following values: normal, breadth-first,
133 inside-out. The default option is normal. This controls how
134 File::Wildcard handles the ellipsis. The default is a normal depth
135 first search, presenting the name of each containing directory
136 before the contents.
137
138 The inside-out order presents the contents of directories first
139 before the directory, which is useful when you want to remove files
140 and directories (all O/S require directories to be empty before
141 rmdir will work). See t/03_absolute.t as this uses inside-out order
142 to tidy up after the test.
143
144 Breadth-first is rarely needed (but I do have an application for
145 it). Here, the whole directory contents is presented before
146 traversing any subdirectories.
147
148 Consider the following tree: a/ a/bar/ a/bar/drink a/foo/ a/foo/lish
149
150 breadth-first will give the following order: qw(a/ a/bar/ a/foo/
151 a/bar/drink a/foo/lish). normal gives the order in which the files
152 are listed. inside-out gives the following: qw(a/bar/drink a/bar/
153 a/foo/lish a/foo/ a/).
154
155 "sort"
156 By default, globbing returns the list of files in the order in which
157 they are returned by the dirhandle (internally). If you specify sort
158 => 1, the files are sorted into ASCII sequence (case insensitively
159 if we are operating that way). If you specify a CODEREF, this will
160 be used as a comparison routine. Note that this takes its operands
161 in @_, not in $a and $b.
162
163 "debug" and "debug_output"
164 You can enable a trace of the internal states of File::Wildcard by
165 setting debug to a true value. Set debug_output to an open
166 filehandle to get the trace in a file. If you are submitting bug
167 reports for File::Wildcard, attaching debug trace files would be
168 very useful.
169
170 debug_output defaults to STDERR.
171
172 match
173 my $foo_re = $foo->match;
174 $foo->match('bar/core');
175
176 This is a get and set method that gives access to the match regexp that
177 the File::Wildcard object is using. It is possible to change the regex
178 on the fly in the middle of a search (though I don't know why anyone
179 would want to do this).
180
181 append
182 $foo->append(path => '/home/me///*.tmp');
183
184 appends a path to an object's todo list. This will be globbed after the
185 object has finished processing the existing wildcards.
186
187 prepend
188 $srcfnd->prepend(path => $include_file);
189
190 This is similar to append, but prepends the path to the todo list. In
191 other words, the current wildcard operation is interrupted to serve the
192 new path, then the previous wildcard operation is resumed when this is
193 exhausted.
194
195 next
196 while (my $core = $foo->next) {
197 unlink $core;
198 }
199 my ($src,$obj,$hdr) = @{$srcfnd->next};
200
201 The "next" method is an iterator, which returns successive files.
202 Returns matching files if there was no derive option passed to new. If
203 there was a derive option, returns an arrayref containing the matching
204 filespec and all derived filespecs. The derived filespecs do not have to
205 exist.
206
207 Note that "next" maintains an internal cursor, which retains context and
208 state information. Beware if the contents of directories are changing
209 while you are iterating with next; you may get unpredictable results. If
210 you are intending to change the contents of the directories you are
211 scanning (with unlink or rename), you are better off deferring this
212 operation until you have processed the whole tree. For the pending
213 delete or rename operations, you could always use another File::Wildcard
214 object - see the spike example below:
215
216 all
217 my @cores = $foo->all;
218
219 "all" returns an array of matching files, in the simple case. Returns an
220 array of arrays if you are constructing new filenames, like the $srcfnd
221 example.
222
223 Beware of the performance and memory implications of using "all". The
224 method will not return until it has read the entire directory tree. Use
225 of the "all" method is not recommended for traversing large directory
226 trees and whole file systems. Consider coding the traversal using the
227 iterator "next" instead.
228
229 reset
230 "reset" causes the wildcard context to be set to re-read the first
231 filename again. Note that this will cause directory contents to be
232 re-read.
233
234 Note also that this will cause the path to revert to the original path
235 specified to new. Any additional paths appended or prepended will be
236 forgotten.
237
238 close
239 Release all directory handles associated with the File::Wildcard object.
240 An object that has been closed will be garbage collected once it goes
241 out of scope. Wildcards that have been exhausted are automatically
242 closed, (i.e. "all" was used, or c<next> returned undef).
243
244 Subsequent calls to "next" will return undef. It is possible to call
245 "reset" after "close" on the same File::Wildcard object, which will
246 cause it to be reopened.
247
248 EXAMPLES
249 * The spike
250 my $todo = File::Wildcard->new;
251
252 ...
253
254 $todo->append(path => $file);
255
256 ...
257
258 while (my $file = $todo->next) {
259 ...
260 }
261
262 You can use an empty wildcard to store a list of filenames for later
263 processing. The order in which they will be seen depends on whether
264 append or prepend is used.
265
266 * Shell style globbing
267 my $wc_args = File::Wildcard->new;
268
269 $wc_args->append(path => $_) for @ARGV;
270
271 while ($wc_args->next) {
272 ...
273 }
274
275 On Unix, file wildcards on the command line are globbed by the shell
276 before perl sees them, unless the wildcards are escaped or quoted.
277 This is not true of other operating systems. MS-DOS does no globbing
278 at all for example.
279
280 File::Wildcard gives you the bonus of elliptic globbing with '///'.
281
282 CAVEAT
283 This module takes POSIX filenames, which use forward slash '/' as a path
284 separator. All operating systems that run Perl can manage this type of
285 path. The module is not designed to work with native file specs. If you
286 want to write code that is portable, convert native filespecs to the
287 POSIX form. There is of course no difference on Unix platforms.
288
289 BUGS
290 Please report bugs to http://rt.cpan.org
291
292 AUTHOR
293 Ivor Williams
294 ivorw-file-wildcard010 at xemaps.com
295
296 COPYRIGHT
297 This program is free software; you can redistribute it and/or modify it
298 under the same terms as Perl itself.
299
300 The full text of the license can be found in the LICENSE file included
301 with this module.
302
303 SEE ALSO
304 glob(3), File::Find, File::Find::Rule.
305