1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
4 .\" ========================================================================
5 .de Sh \" Subsection heading
13 .de Sp \" Vertical space (when we can't use .PP)
17 .de Vb \" Begin verbatim text
22 .de Ve \" End verbatim text
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<>.
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
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
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.
57 . tm Index:\\$1\t\\n%\t"\\$2"
63 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
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
79 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
85 . \" simple accents for nroff and troff
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'
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 \
129 .\" ========================================================================
131 .IX Title "File::GlobMapper 3"
132 .TH File::GlobMapper 3 "2009-02-04" "perl v5.8.7" "User Contributed Perl Documentation"
134 File::GlobMapper \- Extend File Glob to Allow Input and Output Files
136 .IX Header "SYNOPSIS"
138 \& use File::GlobMapper qw( globmap );
142 \& my $aref = globmap $input => $output
143 \& or die $File::GlobMapper::Error ;
147 \& my $gm = new File::GlobMapper $input => $output
148 \& or die $File::GlobMapper::Error ;
151 .IX Header "DESCRIPTION"
152 This module needs Perl5.005 or better.
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.
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
175 and they need renamed to this
183 Below is a possible implementation of a script to carry out the rename
184 (error cases have been omitted)
187 \& foreach my $old ( glob "*.tar.gz" )
190 \& $new =~ s#(.*)\e.tar\e.gz$#$1.tgz# ;
194 \& rename $old => $new
195 \& or die "Cannot rename '$old' to '$new': $!\en;
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.
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?
208 Well, that's \fIexactly\fR what \f(CW\*(C`File::GlobMapper\*(C'\fR does.
210 Here is same snippet of code rewritten using \f(CW\*(C`globmap\*(C'\fR
213 \& for my $pair (globmap '<*.tar.gz>' => '<#1.tgz>' )
215 \& my ($from, $to) = @$pair;
216 \& rename $from => $to
217 \& or die "Cannot rename '$old' to '$new': $!\en;
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.
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
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.
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.
242 In this case the input fileglob \f(CW\*(C`*.tar.gz\*(C'\fR will be transformed into
243 this Perl regular expression
246 \& ([^/]*)\e.tar\e.gz
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.
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.
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
268 \& ( ['alpha.tar.gz' => 'alpha.tgz'],
269 \& ['beta.tar.gz' => 'beta.tgz' ],
270 \& ['gamma.tar.gz' => 'gamma.tgz']
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.
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
290 Whitespace does not delimit fileglobs.
292 The use of parenthesis can be used to capture parts of the input filename.
294 If an Input glob matches the same file more than once, only the first
306 Matches a literal '.'.
307 Equivalent to the Perl regular expression
314 Matches zero or more characters, except '/'. Equivalent to the Perl
322 Matches zero or one character, except '/'. Equivalent to the Perl
330 Backslash is used, as usual, to escape the next character.
339 Capturing parenthesis that work just like perl
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.
346 The first is the '*' metacharacter. This will be replaced by the complete
347 filename matched by the input file glob. So
355 Output FileGlobs take the
359 The \*(L"*\*(R" character will be replaced with the complete input filename.
362 Patterns of the form /#\ed/ will be replaced with the
364 .IX Subsection "Returned Data"
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.
373 \& use File::GlobMapper qw(globmap) ;
378 \& die "rename: Usage rename 'from' 'to'\en"
379 \& unless @ARGV == 2 ;
383 \& my $fromGlob = shift @ARGV;
384 \& my $toGlob = shift @ARGV;
388 \& my $pairs = globmap($fromGlob, $toGlob)
389 \& or die $File::GlobMapper::Error;
393 \& for my $pair (@$pairs)
395 \& my ($from, $to) = @$pair;
396 \& move $from => $to ;
400 Here is an example that renames all c files to cpp.
403 \& $ rename '*.c' '#1.cpp'
405 .Sh "A few example globmaps"
406 .IX Subsection "A few example globmaps"
407 Below are a few examples of globmaps
409 To copy all your .c file to a backup directory
412 \& '</my/home/*.c>' '</my/backup/#1.c>'
415 If you want to compress all
418 \& '</my/home/*.[ch]>' '<*.gz>'
424 \& '</my/home/*.[ch].gz>' '</my/home/#1.#2>'
427 .IX Header "SEE ALSO"
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.