blib/lib/ExtUtils/Command.pm

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