lib/ExtUtils/Command.pm

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