Debian Codegraph @

upstream/mageia-cauldron/man3pm/File::GlobMapper.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "File::GlobMapper 3pm"
61 .TH File::GlobMapper 3pm 2024-05-07 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 File::GlobMapper \- Extend File Glob to Allow Input and Output Files
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 1
71 \& use File::GlobMapper qw( globmap );
72 \&
73 \& my $aref = globmap $input => $output
74 \& or die $File::GlobMapper::Error ;
75 \&
76 \& my $gm = File::GlobMapper\->new( $input => $output )
77 \& or die $File::GlobMapper::Error ;
78 .Ve
79 .SH DESCRIPTION
80 .IX Header "DESCRIPTION"
81 This module needs Perl5.005 or better.
82 .PP
83 This module takes the existing \f(CW\*(C`File::Glob\*(C'\fR module as a starting point and
84 extends it to allow new filenames to be derived from the files matched by
85 \&\f(CW\*(C`File::Glob\*(C'\fR.
86 .PP
87 This can be useful when carrying out batch operations on multiple files that
88 have both an input filename and output filename and the output file can be
89 derived from the input filename. Examples of operations where this can be
90 useful include, file renaming, file copying and file compression.
91 .SS "Behind The Scenes"
92 .IX Subsection "Behind The Scenes"
93 To help explain what \f(CW\*(C`File::GlobMapper\*(C'\fR does, consider what code you
94 would write if you wanted to rename all files in the current directory
95 that ended in \f(CW\*(C`.tar.gz\*(C'\fR to \f(CW\*(C`.tgz\*(C'\fR. So say these files are in the
96 current directory
97 .PP
98 .Vb 3
99 \& alpha.tar.gz
100 \& beta.tar.gz
101 \& gamma.tar.gz
102 .Ve
103 .PP
104 and they need renamed to this
105 .PP
106 .Vb 3
107 \& alpha.tgz
108 \& beta.tgz
109 \& gamma.tgz
110 .Ve
111 .PP
112 Below is a possible implementation of a script to carry out the rename
113 (error cases have been omitted)
114 .PP
115 .Vb 4
116 \& foreach my $old ( glob "*.tar.gz" )
117 \& {
118 \& my $new = $old;
119 \& $new =~ s#(.*)\e.tar\e.gz$#$1.tgz# ;
120 \&
121 \& rename $old => $new
122 \& or die "Cannot rename \*(Aq$old\*(Aq to \*(Aq$new\*(Aq: $!\en;
123 \& }
124 .Ve
125 .PP
126 Notice that a file glob pattern \f(CW\*(C`*.tar.gz\*(C'\fR was used to match the
127 \&\f(CW\*(C`.tar.gz\*(C'\fR files, then a fairly similar regular expression was used in
128 the substitute to allow the new filename to be created.
129 .PP
130 Given that the file glob is just a cut\-down regular expression and that it
131 has already done a lot of the hard work in pattern matching the filenames,
132 wouldn\*(Aqt it be handy to be able to use the patterns in the fileglob to
133 drive the new filename?
134 .PP
135 Well, that\*(Aqs \fIexactly\fR what \f(CW\*(C`File::GlobMapper\*(C'\fR does.
136 .PP
137 Here is same snippet of code rewritten using \f(CW\*(C`globmap\*(C'\fR
138 .PP
139 .Vb 6
140 \& for my $pair (globmap \*(Aq<*.tar.gz>\*(Aq => \*(Aq<#1.tgz>\*(Aq )
141 \& {
142 \& my ($from, $to) = @$pair;
143 \& rename $from => $to
144 \& or die "Cannot rename \*(Aq$old\*(Aq to \*(Aq$new\*(Aq: $!\en;
145 \& }
146 .Ve
147 .PP
148 So how does it work?
149 .PP
150 Behind the scenes the \f(CW\*(C`globmap\*(C'\fR function does a combination of a
151 file glob to match existing filenames followed by a substitute
152 to create the new filenames.
153 .PP
154 Notice how both parameters to \f(CW\*(C`globmap\*(C'\fR are strings that are delimited by <>.
155 This is done to make them look more like file globs \- it is just syntactic
156 sugar, but it can be handy when you want the strings to be visually
157 distinctive. The enclosing <> are optional, so you don\*(Aqt have to use them \- in
158 fact the first thing globmap will do is remove these delimiters if they are
159 present.
160 .PP
161 The first parameter to \f(CW\*(C`globmap\*(C'\fR, \f(CW\*(C`*.tar.gz\*(C'\fR, is an \fIInput File Glob\fR.
162 Once the enclosing "< ... >" is removed, this is passed (more or
163 less) unchanged to \f(CW\*(C`File::Glob\*(C'\fR to carry out a file match.
164 .PP
165 Next the fileglob \f(CW\*(C`*.tar.gz\*(C'\fR is transformed behind the scenes into a
166 full Perl regular expression, with the additional step of wrapping each
167 transformed wildcard metacharacter sequence in parenthesis.
168 .PP
169 In this case the input fileglob \f(CW\*(C`*.tar.gz\*(C'\fR will be transformed into
170 this Perl regular expression
171 .PP
172 .Vb 1
173 \& ([^/]*)\e.tar\e.gz
174 .Ve
175 .PP
176 Wrapping with parenthesis allows the wildcard parts of the Input File
177 Glob to be referenced by the second parameter to \f(CW\*(C`globmap\*(C'\fR, \f(CW\*(C`#1.tgz\*(C'\fR,
178 the \fIOutput File Glob\fR. This parameter operates just like the replacement
179 part of a substitute command. The difference is that the \f(CW\*(C`#1\*(C'\fR syntax
180 is used to reference sub\-patterns matched in the input fileglob, rather
181 than the \f(CW$1\fR syntax that is used with perl regular expressions. In
182 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
183 Input File Glob. This makes it easier to use this module where the
184 parameters to \f(CW\*(C`globmap\*(C'\fR are typed at the command line.
185 .PP
186 The final step involves passing each filename matched by the \f(CW\*(C`*.tar.gz\*(C'\fR
187 file glob through the derived Perl regular expression in turn and
188 expanding the output fileglob using it.
189 .PP
190 The end result of all this is a list of pairs of filenames. By default
191 that is what is returned by \f(CW\*(C`globmap\*(C'\fR. In this example the data structure
192 returned will look like this
193 .PP
194 .Vb 4
195 \& ( [\*(Aqalpha.tar.gz\*(Aq => \*(Aqalpha.tgz\*(Aq],
196 \& [\*(Aqbeta.tar.gz\*(Aq => \*(Aqbeta.tgz\*(Aq ],
197 \& [\*(Aqgamma.tar.gz\*(Aq => \*(Aqgamma.tgz\*(Aq]
198 \& )
199 .Ve
200 .PP
201 Each pair is an array reference with two elements \- namely the \fIfrom\fR
202 filename, that \f(CW\*(C`File::Glob\*(C'\fR has matched, and a \fIto\fR filename that is
203 derived from the \fIfrom\fR filename.
204 .SS Limitations
205 .IX Subsection "Limitations"
206 \&\f(CW\*(C`File::GlobMapper\*(C'\fR has been kept simple deliberately, so it isn\*(Aqt intended to
207 solve all filename mapping operations. Under the hood \f(CW\*(C`File::Glob\*(C'\fR (or for
208 older versions of Perl, \f(CW\*(C`File::BSDGlob\*(C'\fR) is used to match the files, so you
209 will never have the flexibility of full Perl regular expression.
210 .SS "Input File Glob"
211 .IX Subsection "Input File Glob"
212 The syntax for an Input FileGlob is identical to \f(CW\*(C`File::Glob\*(C'\fR, except
213 for the following
214 .IP 1. 5
215 No nested {}
216 .IP 2. 5
217 Whitespace does not delimit fileglobs.
218 .IP 3. 5
219 The use of parenthesis can be used to capture parts of the input filename.
220 .IP 4. 5
221 If an Input glob matches the same file more than once, only the first
222 will be used.
223 .PP
224 The syntax
225 .IP \fB~\fR 5
226 .IX Item "~"
227 .PD 0
228 .IP \fB~user\fR 5
229 .IX Item "~user"
230 .IP \fB.\fR 5
231 .IX Item "."
232 .PD
233 Matches a literal \*(Aq.\*(Aq.
234 Equivalent to the Perl regular expression
235 .Sp
236 .Vb 1
237 \& \e.
238 .Ve
239 .IP \fB*\fR 5
240 .IX Item "*"
241 Matches zero or more characters, except \*(Aq/\*(Aq. Equivalent to the Perl
242 regular expression
243 .Sp
244 .Vb 1
245 \& [^/]*
246 .Ve
247 .IP \fB?\fR 5
248 .IX Item "?"
249 Matches zero or one character, except \*(Aq/\*(Aq. Equivalent to the Perl
250 regular expression
251 .Sp
252 .Vb 1
253 \& [^/]?
254 .Ve
255 .IP \fB\e\fR 5
256 .IX Item ""
257 Backslash is used, as usual, to escape the next character.
258 .IP \fB[]\fR 5
259 .IX Item "[]"
260 Character class.
261 .IP \fB{,}\fR 5
262 .IX Item "{,}"
263 Alternation
264 .IP \fB()\fR 5
265 .IX Item "()"
266 Capturing parenthesis that work just like perl
267 .PP
268 Any other character it taken literally.
269 .SS "Output File Glob"
270 .IX Subsection "Output File Glob"
271 The Output File Glob is a normal string, with 2 glob\-like features.
272 .PP
273 The first is the \*(Aq*\*(Aq metacharacter. This will be replaced by the complete
274 filename matched by the input file glob. So
275 .PP
276 .Vb 1
277 \& *.c *.Z
278 .Ve
279 .PP
280 The second is
281 .PP
282 Output FileGlobs take the
283 .IP """*""" 5
284 The "*" character will be replaced with the complete input filename.
285 .IP #1 5
286 .IX Item "#1"
287 Patterns of the form /#\ed/ will be replaced with the
288 .SS "Returned Data"
289 .IX Subsection "Returned Data"
290 .SH EXAMPLES
291 .IX Header "EXAMPLES"
292 .SS "A Rename script"
293 .IX Subsection "A Rename script"
294 Below is a simple "rename" script that uses \f(CW\*(C`globmap\*(C'\fR to determine the
295 source and destination filenames.
296 .PP
297 .Vb 2
298 \& use File::GlobMapper qw(globmap) ;
299 \& use File::Copy;
300 \&
301 \& die "rename: Usage rename \*(Aqfrom\*(Aq \*(Aqto\*(Aq\en"
302 \& unless @ARGV == 2 ;
303 \&
304 \& my $fromGlob = shift @ARGV;
305 \& my $toGlob = shift @ARGV;
306 \&
307 \& my $pairs = globmap($fromGlob, $toGlob)
308 \& or die $File::GlobMapper::Error;
309 \&
310 \& for my $pair (@$pairs)
311 \& {
312 \& my ($from, $to) = @$pair;
313 \& move $from => $to ;
314 \& }
315 .Ve
316 .PP
317 Here is an example that renames all c files to cpp.
318 .PP
319 .Vb 1
320 \& $ rename \*(Aq*.c\*(Aq \*(Aq#1.cpp\*(Aq
321 .Ve
322 .SS "A few example globmaps"
323 .IX Subsection "A few example globmaps"
324 Below are a few examples of globmaps
325 .PP
326 To copy all your .c file to a backup directory
327 .PP
328 .Vb 1
329 \& \*(Aq</my/home/*.c>\*(Aq \*(Aq</my/backup/#1.c>\*(Aq
330 .Ve
331 .PP
332 If you want to compress all
333 .PP
334 .Vb 1
335 \& \*(Aq</my/home/*.[ch]>\*(Aq \*(Aq<*.gz>\*(Aq
336 .Ve
337 .PP
338 To uncompress
339 .PP
340 .Vb 1
341 \& \*(Aq</my/home/*.[ch].gz>\*(Aq \*(Aq</my/home/#1.#2>\*(Aq
342 .Ve
343 .SH "SEE ALSO"
344 .IX Header "SEE ALSO"
345 File::Glob
346 .SH AUTHOR
347 .IX Header "AUTHOR"
348 The \fIFile::GlobMapper\fR module was written by Paul Marquess, \[email protected]\fR.
349 .SH "COPYRIGHT AND LICENSE"
350 .IX Header "COPYRIGHT AND LICENSE"
351 Copyright (c) 2005 Paul Marquess. All rights reserved.
352 This program is free software; you can redistribute it and/or
353 modify it under the same terms as Perl itself.