blib/lib/ExtUtils/Command.pm

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