blib/lib/ExtUtils/Command.pm untested

filter: Statementstmt 0.0% 0 / 161 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 31 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 276 CC 17 SCAR 22.1
CC
17
Cov
0%
dos2unix
30.0
rm_f
20.0
cp
12.0
CRAP 9.1
1 package ExtUtils::Command;
2  
3 use 5.00503;
4 use strict;
5 require Exporter;
6 use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
7 @ISA = qw(Exporter);
8 @EXPORT = qw(cp rm_f rm_rf mv cat eqtime mkpath touch test_f test_d chmod
9 dos2unix);
10 $VERSION = '7.13_01';
11 $VERSION = eval $VERSION;
12  
13 my $Is_VMS = $^O eq 'VMS';
14 my $Is_VMS_mode = $Is_VMS;
15 my $Is_VMS_noefs = $Is_VMS;
16 my $Is_Win32 = $^O eq 'MSWin32';
17  
18 if( $Is_VMS ) {
19 my $vms_unix_rpt;
20 my $vms_efs;
21 my $vms_case;
22  
23 if (eval { local $SIG{__DIE__}; require VMS::Feature; }) {
24 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
25 $vms_efs = VMS::Feature::current("efs_charset");
26 $vms_case = VMS::Feature::current("efs_case_preserve");
27 } else {
28 my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
29 my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
30 my $efs_case = $ENV{'DECC$EFS_CASE_PRESERVE'} || '';
31 $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
32 $vms_efs = $efs_charset =~ /^[ET1]/i;
33 $vms_case = $efs_case =~ /^[ET1]/i;
34 }
35 $Is_VMS_mode = 0 if $vms_unix_rpt;
36 $Is_VMS_noefs = 0 if ($vms_efs);
37 }
38  
39  
40 =head1 NAME
41  
42 ExtUtils::Command - utilities to replace common UNIX commands in Makefiles etc.
43  
44 =head1 SYNOPSIS
45  
46 perl -MExtUtils::Command -e cat files... > destination
47 perl -MExtUtils::Command -e mv source... destination
48 perl -MExtUtils::Command -e cp source... destination
49 perl -MExtUtils::Command -e touch files...
50 perl -MExtUtils::Command -e rm_f files...
51 perl -MExtUtils::Command -e rm_rf directories...
52 perl -MExtUtils::Command -e mkpath directories...
53 perl -MExtUtils::Command -e eqtime source destination
54 perl -MExtUtils::Command -e test_f file
55 perl -MExtUtils::Command -e test_d directory
56 perl -MExtUtils::Command -e chmod mode files...
57 ...
58  
59 =head1 DESCRIPTION
60  
61 The module is used to replace common UNIX commands. In all cases the
62 functions work from @ARGV rather than taking arguments. This makes
63 them easier to deal with in Makefiles. Call them like this:
64  
65 perl -MExtUtils::Command -e some_command some files to work on
66  
67 and I<NOT> like this:
68  
69 perl -MExtUtils::Command -e 'some_command qw(some files to work on)'
70  
71 For that use L<Shell::Command>.
72  
73 Filenames with * and ? will be glob expanded.
74  
75  
76 =head2 FUNCTIONS
77  
78 =over 4
79  
80 =cut
81  
82 # VMS uses % instead of ? to mean "one character"
83 my $wild_regex = $Is_VMS ? '*%' : '*?';
84 sub expand_wildcards
85 {
86 @ARGV = map(/[$wild_regex]/o ? glob($_) : $_,@ARGV);
87 }
88  
89  
90 =item cat
91  
92 cat file ...
93  
94 Concatenates all files mentioned on command line to STDOUT.
95  
96 =cut
97  
98 sub cat ()
99 {
100 expand_wildcards();
101 print while (<>);
102 }
103  
104 =item eqtime
105  
106 eqtime source destination
107  
108 Sets modified time of destination to that of source.
109  
110 =cut
111  
112 sub eqtime
113 {
114 my ($src,$dst) = @ARGV;
115 local @ARGV = ($dst); touch(); # in case $dst doesn't exist
116 utime((stat($src))[8,9],$dst);
117 }
118  
119 =item rm_rf
120  
121 rm_rf files or directories ...
122  
123 Removes files and directories - recursively (even if readonly)
124  
125 =cut
126  
127 sub rm_rf
128 {
129 expand_wildcards();
130 require File::Path;
131 File::Path::rmtree([grep -e $_,@ARGV],0,0);
132 }
133  
134 =item rm_f
135  
136 rm_f file ...
137  
138 Removes files (even if readonly)
139  
140 =cut
141  
142 sub rm_f {
143 expand_wildcards();
144  
145 foreach my $file (@ARGV) {
146 next unless -f $file;
147  
148 next if _unlink($file);
149  
150 chmod(0777, $file);
151  
152 next if _unlink($file);
153  
154 require Carp;
155 Carp::carp("Cannot delete $file: $!");
156 }
157 }
158  
159 sub _unlink {
160 my $files_unlinked = 0;
161 foreach my $file (@_) {
162 my $delete_count = 0;
163 $delete_count++ while unlink $file;
164 $files_unlinked++ if $delete_count;
165 }
166 return $files_unlinked;
167 }
168  
169  
170 =item touch
171  
172 touch file ...
173  
174 Makes files exist, with current timestamp
175  
176 =cut
177  
178 sub touch {
179 my $t = time;
180 expand_wildcards();
181 foreach my $file (@ARGV) {
182 open(FILE,">>$file") || die "Cannot write $file:$!";
183 close(FILE);
184 utime($t,$t,$file);
185 }
186 }
187  
188 =item mv
189  
190 mv source_file destination_file
191 mv source_file source_file destination_dir
192  
193 Moves source to destination. Multiple sources are allowed if
194 destination is an existing directory.
195  
196 Returns true if all moves succeeded, false otherwise.
197  
198 =cut
199  
200 sub mv {
201 expand_wildcards();
202 my @src = @ARGV;
203 my $dst = pop @src;
204  
205 if (@src > 1 && ! -d $dst) {
206 require Carp;
207 Carp::croak("Too many arguments");
208 }
209  
210 require File::Copy;
211 my $nok = 0;
212 foreach my $src (@src) {
213 $nok ||= !File::Copy::move($src,$dst);
214 }
215 return !$nok;
216 }
217  
218 =item cp
219  
220 cp source_file destination_file
221 cp source_file source_file destination_dir
222  
223 Copies sources to the destination. Multiple sources are allowed if
224 destination is an existing directory.
225  
226 Returns true if all copies succeeded, false otherwise.
227  
228 =cut
229  
230 sub cp {
231 expand_wildcards();
232 my @src = @ARGV;
233 my $dst = pop @src;
234  
235 if (@src > 1 && ! -d $dst) {
236 require Carp;
237 Carp::croak("Too many arguments");
238 }
239  
240 require File::Copy;
241 my $nok = 0;
242 foreach my $src (@src) {
243 $nok ||= !File::Copy::copy($src,$dst);
244  
245 # Win32 does not update the mod time of a copied file, just the
246 # created time which make does not look at.
247 utime(time, time, $dst) if $Is_Win32;
248 }
249 return $nok;
250 }
251  
252 =item chmod
253  
254 chmod mode files ...
255  
256 Sets UNIX like permissions 'mode' on all the files. e.g. 0666
257  
258 =cut
259  
260 sub chmod {
261 local @ARGV = @ARGV;
262 my $mode = shift(@ARGV);
263 expand_wildcards();
264  
265 if( $Is_VMS_mode && $Is_VMS_noefs) {
266 require File::Spec;
267 foreach my $idx (0..$#ARGV) {
268 my $path = $ARGV[$idx];
269 next unless -d $path;
270  
271 # chmod 0777, [.foo.bar] doesn't work on VMS, you have to do
272 # chmod 0777, [.foo]bar.dir
273 my @dirs = File::Spec->splitdir( $path );
274 $dirs[-1] .= '.dir';
275 $path = File::Spec->catfile(@dirs);
276  
277 $ARGV[$idx] = $path;
278 }
279 }
280  
281 chmod(oct $mode,@ARGV) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
282 }
283  
284 =item mkpath
285  
286 mkpath directory ...
287  
288 Creates directories, including any parent directories.
289  
290 =cut
291  
292 sub mkpath
293 {
294 expand_wildcards();
295 require File::Path;
296 File::Path::mkpath([@ARGV],0,0777);
297 }
298  
299 =item test_f
300  
301 test_f file
302  
303 Tests if a file exists. I<Exits> with 0 if it does, 1 if it does not (ie.
304 shell's idea of true and false).
305  
306 =cut
307  
308 sub test_f
309 {
310 exit(-f $ARGV[0] ? 0 : 1);
311 }
312  
313 =item test_d
314  
315 test_d directory
316  
317 Tests if a directory exists. I<Exits> with 0 if it does, 1 if it does
318 not (ie. shell's idea of true and false).
319  
320 =cut
321  
322 sub test_d
323 {
324 exit(-d $ARGV[0] ? 0 : 1);
325 }
326  
327 =item dos2unix
328  
329 dos2unix files or dirs ...
330  
331 Converts DOS and OS/2 linefeeds to Unix style recursively.
332  
333 =cut
334  
335 sub dos2unix {
336 require File::Find;
337 File::Find::find(sub {
338 return if -d;
339 return unless -w _;
340 return unless -r _;
341 return if -B _;
342  
343 local $\;
344  
345 my $orig = $_;
346 my $temp = '.dos2unix_tmp';
347 open ORIG, $_ or do { warn "dos2unix can't open $_: $!"; return };
348 open TEMP, ">$temp" or
349 do { warn "dos2unix can't create .dos2unix_tmp: $!"; return };
350 binmode ORIG; binmode TEMP;
351 while (my $line = <ORIG>) {
352 $line =~ s/\015\012/\012/g;
353 print TEMP $line;
354 }
355 close ORIG;
356 close TEMP;
357 rename $temp, $orig;
358  
359 }, @ARGV);
360 }
361  
362 =back
363  
364 =head1 SEE ALSO
365  
366 Shell::Command which is these same functions but take arguments normally.
367  
368  
369 =head1 AUTHOR
370  
371 Nick Ing-Simmons C<ni-s@cpan.org>
372  
373 Maintained by Michael G Schwern C<schwern@pobox.com> within the
374 ExtUtils-MakeMaker package and, as a separate CPAN package, by
375 Randy Kobes C<r.kobes@uwinnipeg.ca>.
376  
377 =cut
378