Debian Codegraph @

upstream/debian-unstable/man3/File::GlobMapper.3perl

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