Commit | Line | Data |
3fea05b9 |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3 |
2 | .\" |
3 | .\" Standard preamble: |
4 | .\" ======================================================================== |
5 | .de Sh \" Subsection heading |
6 | .br |
7 | .if t .Sp |
8 | .ne 5 |
9 | .PP |
10 | \fB\\$1\fR |
11 | .PP |
12 | .. |
13 | .de Sp \" Vertical space (when we can't use .PP) |
14 | .if t .sp .5v |
15 | .if n .sp |
16 | .. |
17 | .de Vb \" Begin verbatim text |
18 | .ft CW |
19 | .nf |
20 | .ne \\$1 |
21 | .. |
22 | .de Ve \" End verbatim text |
23 | .ft R |
24 | .fi |
25 | .. |
26 | .\" Set up some character translations and predefined strings. \*(-- will |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
32 | .tr \(*W-|\(bv\*(Tr |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
34 | .ie n \{\ |
35 | . ds -- \(*W- |
36 | . ds PI pi |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
39 | . ds L" "" |
40 | . ds R" "" |
41 | . ds C` "" |
42 | . ds C' "" |
43 | 'br\} |
44 | .el\{\ |
45 | . ds -- \|\(em\| |
46 | . ds PI \(*p |
47 | . ds L" `` |
48 | . ds R" '' |
49 | 'br\} |
50 | .\" |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
54 | .\" output yourself in some meaningful fashion. |
55 | .if \nF \{\ |
56 | . de IX |
57 | . tm Index:\\$1\t\\n%\t"\\$2" |
58 | .. |
59 | . nr % 0 |
60 | . rr F |
61 | .\} |
62 | .\" |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
64 | .\" way too many mistakes in technical documents. |
65 | .hy 0 |
66 | .if n .na |
67 | .\" |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
70 | . \" fudge factors for nroff and troff |
71 | .if n \{\ |
72 | . ds #H 0 |
73 | . ds #V .8m |
74 | . ds #F .3m |
75 | . ds #[ \f1 |
76 | . ds #] \fP |
77 | .\} |
78 | .if t \{\ |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
80 | . ds #V .6m |
81 | . ds #F 0 |
82 | . ds #[ \& |
83 | . ds #] \& |
84 | .\} |
85 | . \" simple accents for nroff and troff |
86 | .if n \{\ |
87 | . ds ' \& |
88 | . ds ` \& |
89 | . ds ^ \& |
90 | . ds , \& |
91 | . ds ~ ~ |
92 | . ds / |
93 | .\} |
94 | .if t \{\ |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
101 | .\} |
102 | . \" troff and (daisy-wheel) nroff accents |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
110 | .ds ae a\h'-(\w'a'u*4/10)'e |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
112 | . \" corrections for vroff |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
115 | . \" for low resolution devices (crt and lpr) |
116 | .if \n(.H>23 .if \n(.V>19 \ |
117 | \{\ |
118 | . ds : e |
119 | . ds 8 ss |
120 | . ds o a |
121 | . ds d- d\h'-1'\(ga |
122 | . ds D- D\h'-1'\(hy |
123 | . ds th \o'bp' |
124 | . ds Th \o'LP' |
125 | . ds ae ae |
126 | . ds Ae AE |
127 | .\} |
128 | .rm #[ #] #H #V #F C |
129 | .\" ======================================================================== |
130 | .\" |
131 | .IX Title "File::GlobMapper 3" |
132 | .TH File::GlobMapper 3 "2009-02-04" "perl v5.8.7" "User Contributed Perl Documentation" |
133 | .SH "NAME" |
134 | File::GlobMapper \- Extend File Glob to Allow Input and Output Files |
135 | .SH "SYNOPSIS" |
136 | .IX Header "SYNOPSIS" |
137 | .Vb 1 |
138 | \& use File::GlobMapper qw( globmap ); |
139 | .Ve |
140 | .PP |
141 | .Vb 2 |
142 | \& my $aref = globmap $input => $output |
143 | \& or die $File::GlobMapper::Error ; |
144 | .Ve |
145 | .PP |
146 | .Vb 2 |
147 | \& my $gm = new File::GlobMapper $input => $output |
148 | \& or die $File::GlobMapper::Error ; |
149 | .Ve |
150 | .SH "DESCRIPTION" |
151 | .IX Header "DESCRIPTION" |
152 | This module needs Perl5.005 or better. |
153 | .PP |
154 | This module takes the existing \f(CW\*(C`File::Glob\*(C'\fR module as a starting point and |
155 | extends it to allow new filenames to be derived from the files matched by |
156 | \&\f(CW\*(C`File::Glob\*(C'\fR. |
157 | .PP |
158 | This can be useful when carrying out batch operations on multiple files that |
159 | have both an input filename and output filename and the output file can be |
160 | derived from the input filename. Examples of operations where this can be |
161 | useful include, file renaming, file copying and file compression. |
162 | .Sh "Behind The Scenes" |
163 | .IX Subsection "Behind The Scenes" |
164 | To help explain what \f(CW\*(C`File::GlobMapper\*(C'\fR does, consider what code you |
165 | would write if you wanted to rename all files in the current directory |
166 | that ended in \f(CW\*(C`.tar.gz\*(C'\fR to \f(CW\*(C`.tgz\*(C'\fR. So say these files are in the |
167 | current directory |
168 | .PP |
169 | .Vb 3 |
170 | \& alpha.tar.gz |
171 | \& beta.tar.gz |
172 | \& gamma.tar.gz |
173 | .Ve |
174 | .PP |
175 | and they need renamed to this |
176 | .PP |
177 | .Vb 3 |
178 | \& alpha.tgz |
179 | \& beta.tgz |
180 | \& gamma.tgz |
181 | .Ve |
182 | .PP |
183 | Below is a possible implementation of a script to carry out the rename |
184 | (error cases have been omitted) |
185 | .PP |
186 | .Vb 4 |
187 | \& foreach my $old ( glob "*.tar.gz" ) |
188 | \& { |
189 | \& my $new = $old; |
190 | \& $new =~ s#(.*)\e.tar\e.gz$#$1.tgz# ; |
191 | .Ve |
192 | .PP |
193 | .Vb 3 |
194 | \& rename $old => $new |
195 | \& or die "Cannot rename '$old' to '$new': $!\en; |
196 | \& } |
197 | .Ve |
198 | .PP |
199 | Notice that a file glob pattern \f(CW\*(C`*.tar.gz\*(C'\fR was used to match the |
200 | \&\f(CW\*(C`.tar.gz\*(C'\fR files, then a fairly similar regular expression was used in |
201 | the substitute to allow the new filename to be created. |
202 | .PP |
203 | Given that the file glob is just a cut-down regular expression and that it |
204 | has already done a lot of the hard work in pattern matching the filenames, |
205 | wouldn't it be handy to be able to use the patterns in the fileglob to |
206 | drive the new filename? |
207 | .PP |
208 | Well, that's \fIexactly\fR what \f(CW\*(C`File::GlobMapper\*(C'\fR does. |
209 | .PP |
210 | Here is same snippet of code rewritten using \f(CW\*(C`globmap\*(C'\fR |
211 | .PP |
212 | .Vb 6 |
213 | \& for my $pair (globmap '<*.tar.gz>' => '<#1.tgz>' ) |
214 | \& { |
215 | \& my ($from, $to) = @$pair; |
216 | \& rename $from => $to |
217 | \& or die "Cannot rename '$old' to '$new': $!\en; |
218 | \& } |
219 | .Ve |
220 | .PP |
221 | So how does it work? |
222 | .PP |
223 | Behind the scenes the \f(CW\*(C`globmap\*(C'\fR function does a combination of a |
224 | file glob to match existing filenames followed by a substitute |
225 | to create the new filenames. |
226 | .PP |
227 | Notice how both parameters to \f(CW\*(C`globmap\*(C'\fR are strings that are delimited by <>. |
228 | This is done to make them look more like file globs \- it is just syntactic |
229 | sugar, but it can be handy when you want the strings to be visually |
230 | distinctive. The enclosing <> are optional, so you don't have to use them \- in |
231 | fact the first thing globmap will do is remove these delimiters if they are |
232 | present. |
233 | .PP |
234 | The first parameter to \f(CW\*(C`globmap\*(C'\fR, \f(CW\*(C`*.tar.gz\*(C'\fR, is an \fIInput File Glob\fR. |
235 | Once the enclosing \*(L"< ... >\*(R" is removed, this is passed (more or |
236 | less) unchanged to \f(CW\*(C`File::Glob\*(C'\fR to carry out a file match. |
237 | .PP |
238 | Next the fileglob \f(CW\*(C`*.tar.gz\*(C'\fR is transformed behind the scenes into a |
239 | full Perl regular expression, with the additional step of wrapping each |
240 | transformed wildcard metacharacter sequence in parenthesis. |
241 | .PP |
242 | In this case the input fileglob \f(CW\*(C`*.tar.gz\*(C'\fR will be transformed into |
243 | this Perl regular expression |
244 | .PP |
245 | .Vb 1 |
246 | \& ([^/]*)\e.tar\e.gz |
247 | .Ve |
248 | .PP |
249 | Wrapping with parenthesis allows the wildcard parts of the Input File |
250 | Glob to be referenced by the second parameter to \f(CW\*(C`globmap\*(C'\fR, \f(CW\*(C`#1.tgz\*(C'\fR, |
251 | the \fIOutput File Glob\fR. This parameter operates just like the replacement |
252 | part of a substitute command. The difference is that the \f(CW\*(C`#1\*(C'\fR syntax |
253 | is used to reference sub-patterns matched in the input fileglob, rather |
254 | than the \f(CW$1\fR syntax that is used with perl regular expressions. In |
255 | this case \f(CW\*(C`#1\*(C'\fR is used to refer to the text matched by the \f(CW\*(C`*\*(C'\fR in the |
256 | Input File Glob. This makes it easier to use this module where the |
257 | parameters to \f(CW\*(C`globmap\*(C'\fR are typed at the command line. |
258 | .PP |
259 | The final step involves passing each filename matched by the \f(CW\*(C`*.tar.gz\*(C'\fR |
260 | file glob through the derived Perl regular expression in turn and |
261 | expanding the output fileglob using it. |
262 | .PP |
263 | The end result of all this is a list of pairs of filenames. By default |
264 | that is what is returned by \f(CW\*(C`globmap\*(C'\fR. In this example the data structure |
265 | returned will look like this |
266 | .PP |
267 | .Vb 4 |
268 | \& ( ['alpha.tar.gz' => 'alpha.tgz'], |
269 | \& ['beta.tar.gz' => 'beta.tgz' ], |
270 | \& ['gamma.tar.gz' => 'gamma.tgz'] |
271 | \& ) |
272 | .Ve |
273 | .PP |
274 | Each pair is an array reference with two elements \- namely the \fIfrom\fR |
275 | filename, that \f(CW\*(C`File::Glob\*(C'\fR has matched, and a \fIto\fR filename that is |
276 | derived from the \fIfrom\fR filename. |
277 | .Sh "Limitations" |
278 | .IX Subsection "Limitations" |
279 | \&\f(CW\*(C`File::GlobMapper\*(C'\fR has been kept simple deliberately, so it isn't intended to |
280 | solve all filename mapping operations. Under the hood \f(CW\*(C`File::Glob\*(C'\fR (or for |
281 | older versions of Perl, \f(CW\*(C`File::BSDGlob\*(C'\fR) is used to match the files, so you |
282 | will never have the flexibility of full Perl regular expression. |
283 | .Sh "Input File Glob" |
284 | .IX Subsection "Input File Glob" |
285 | The syntax for an Input FileGlob is identical to \f(CW\*(C`File::Glob\*(C'\fR, except |
286 | for the following |
287 | .IP "1." 5 |
288 | No nested {} |
289 | .IP "2." 5 |
290 | Whitespace does not delimit fileglobs. |
291 | .IP "3." 5 |
292 | The use of parenthesis can be used to capture parts of the input filename. |
293 | .IP "4." 5 |
294 | If an Input glob matches the same file more than once, only the first |
295 | will be used. |
296 | .PP |
297 | The syntax |
298 | .IP "\fB~\fR" 5 |
299 | .IX Item "~" |
300 | .PD 0 |
301 | .IP "\fB~user\fR" 5 |
302 | .IX Item "~user" |
303 | .IP "\fB.\fR" 5 |
304 | .IX Item "." |
305 | .PD |
306 | Matches a literal '.'. |
307 | Equivalent to the Perl regular expression |
308 | .Sp |
309 | .Vb 1 |
310 | \& \e. |
311 | .Ve |
312 | .IP "\fB*\fR" 5 |
313 | .IX Item "*" |
314 | Matches zero or more characters, except '/'. Equivalent to the Perl |
315 | regular expression |
316 | .Sp |
317 | .Vb 1 |
318 | \& [^/]* |
319 | .Ve |
320 | .IP "\fB?\fR" 5 |
321 | .IX Item "?" |
322 | Matches zero or one character, except '/'. Equivalent to the Perl |
323 | regular expression |
324 | .Sp |
325 | .Vb 1 |
326 | \& [^/]? |
327 | .Ve |
328 | .IP "\fB\e\fR" 5 |
329 | .IX Item "" |
330 | Backslash is used, as usual, to escape the next character. |
331 | .IP "\fB[]\fR" 5 |
332 | .IX Item "[]" |
333 | Character class. |
334 | .IP "\fB{,}\fR" 5 |
335 | .IX Item "{,}" |
336 | Alternation |
337 | .IP "\fB()\fR" 5 |
338 | .IX Item "()" |
339 | Capturing parenthesis that work just like perl |
340 | .PP |
341 | Any other character it taken literally. |
342 | .Sh "Output File Glob" |
343 | .IX Subsection "Output File Glob" |
344 | The Output File Glob is a normal string, with 2 glob-like features. |
345 | .PP |
346 | The first is the '*' metacharacter. This will be replaced by the complete |
347 | filename matched by the input file glob. So |
348 | .PP |
349 | .Vb 1 |
350 | \& *.c *.Z |
351 | .Ve |
352 | .PP |
353 | The second is |
354 | .PP |
355 | Output FileGlobs take the |
356 | .ie n .IP """*""" 5 |
357 | .el .IP "``*''" 5 |
358 | .IX Item "*" |
359 | The \*(L"*\*(R" character will be replaced with the complete input filename. |
360 | .IP "#1" 5 |
361 | .IX Item "#1" |
362 | Patterns of the form /#\ed/ will be replaced with the |
363 | .Sh "Returned Data" |
364 | .IX Subsection "Returned Data" |
365 | .SH "EXAMPLES" |
366 | .IX Header "EXAMPLES" |
367 | .Sh "A Rename script" |
368 | .IX Subsection "A Rename script" |
369 | Below is a simple \*(L"rename\*(R" script that uses \f(CW\*(C`globmap\*(C'\fR to determine the |
370 | source and destination filenames. |
371 | .PP |
372 | .Vb 2 |
373 | \& use File::GlobMapper qw(globmap) ; |
374 | \& use File::Copy; |
375 | .Ve |
376 | .PP |
377 | .Vb 2 |
378 | \& die "rename: Usage rename 'from' 'to'\en" |
379 | \& unless @ARGV == 2 ; |
380 | .Ve |
381 | .PP |
382 | .Vb 2 |
383 | \& my $fromGlob = shift @ARGV; |
384 | \& my $toGlob = shift @ARGV; |
385 | .Ve |
386 | .PP |
387 | .Vb 2 |
388 | \& my $pairs = globmap($fromGlob, $toGlob) |
389 | \& or die $File::GlobMapper::Error; |
390 | .Ve |
391 | .PP |
392 | .Vb 5 |
393 | \& for my $pair (@$pairs) |
394 | \& { |
395 | \& my ($from, $to) = @$pair; |
396 | \& move $from => $to ; |
397 | \& } |
398 | .Ve |
399 | .PP |
400 | Here is an example that renames all c files to cpp. |
401 | .PP |
402 | .Vb 1 |
403 | \& $ rename '*.c' '#1.cpp' |
404 | .Ve |
405 | .Sh "A few example globmaps" |
406 | .IX Subsection "A few example globmaps" |
407 | Below are a few examples of globmaps |
408 | .PP |
409 | To copy all your .c file to a backup directory |
410 | .PP |
411 | .Vb 1 |
412 | \& '</my/home/*.c>' '</my/backup/#1.c>' |
413 | .Ve |
414 | .PP |
415 | If you want to compress all |
416 | .PP |
417 | .Vb 1 |
418 | \& '</my/home/*.[ch]>' '<*.gz>' |
419 | .Ve |
420 | .PP |
421 | To uncompress |
422 | .PP |
423 | .Vb 1 |
424 | \& '</my/home/*.[ch].gz>' '</my/home/#1.#2>' |
425 | .Ve |
426 | .SH "SEE ALSO" |
427 | .IX Header "SEE ALSO" |
428 | File::Glob |
429 | .SH "AUTHOR" |
430 | .IX Header "AUTHOR" |
431 | The \fIFile::GlobMapper\fR module was written by Paul Marquess, \fIpmqs@cpan.org\fR. |
432 | .SH "COPYRIGHT AND LICENSE" |
433 | .IX Header "COPYRIGHT AND LICENSE" |
434 | Copyright (c) 2005 Paul Marquess. All rights reserved. |
435 | This program is free software; you can redistribute it and/or |
436 | modify it under the same terms as Perl itself. |