lib/ExtUtils/Command.pm

filter: Statementstmt 75.1% 97 / 129 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.6% 169 / 250 CC 35 SCAR 18.2
CC
35
Cov
67%
chmod
15.3
rm_f
9.7
__ANON__
8.8
CRAP 6.2
« blib/lib/ExtUtils/MM_OS2.pm ↑ Summary lib/ExtUtils/MM.pm »
1 package ExtUtils::Command;
2  
3 4 ▶use 5.00503;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 require Exporter;
6 4 ▶use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 @ISA = qw(Exporter);
8 4 @EXPORT = qw(cp rm_f rm_rf mv cat eqtime mkpath touch test_f test_d chmod
9 dos2unix);
10 4 $VERSION = '7.13_01';
11 4 $VERSION = eval $VERSION;
12  
13 4 my $Is_VMS = $^O eq 'VMS';
14 4 my $Is_VMS_mode = $Is_VMS;
15 4 my $Is_VMS_noefs = $Is_VMS;
16 4 my $Is_Win32 = $^O eq 'MSWin32';
17  
18 4 ▶if( $Is_VMS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Is_VMS) 0 4 4
19 0 my $vms_unix_rpt;
20 my $vms_efs;
21 0 my $vms_case;
22  
23 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
24 0 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
25 0 $vms_efs = VMS::Feature::current("efs_charset");
26 0 $vms_case = VMS::Feature::current("efs_case_preserve");
27 } else {
28 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
29 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
30 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
31 0 $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
32 0 $vms_efs = $efs_charset =~ /^[ET1]/i;
33 0 $vms_case = $efs_case =~ /^[ET1]/i;
34 }
35 0 ▶ $Is_VMS_mode = 0 if $vms_unix_rpt;
Branch: 1 branch
BranchTrueFalseTotal
if $vms_unix_rpt 0 0 0
36 0 ▶ $Is_VMS_noefs = 0 if ($vms_efs);
Branch: 1 branch
BranchTrueFalseTotal
if $vms_efs 0 0 0
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 4 ▶my $wild_regex = $Is_VMS ? '*%' : '*?';
Branch: 1 branch
BranchTrueFalseTotal
$Is_VMS ? : 0 4 4
84 sub expand_wildcards
85 {
86 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 98 100
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 1 ▶ expand_wildcards();
Subroutine cat: called CC=2 SCAR=6.9
Pod documented
101 1 ▶ print while (<>);
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline ARGV) and do { print $_ } 568 1 569
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 2 ▶ my ($src,$dst) = @ARGV;
Subroutine eqtime: called CC=1 SCAR=0.0
Pod documented
115 2 local @ARGV = ($dst); touch(); # in case $dst doesn't exist
116 2 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 2 ▶ expand_wildcards();
Subroutine rm_rf: called CC=1 SCAR=0.0
Pod documented
130 2 require File::Path;
131 2 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 1 ▶ expand_wildcards();
Subroutine rm_f: called CC=5 SCAR=22.7
Pod documented
144  
145 1 foreach my $file (@ARGV) {
146 2 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 2 2
147  
148 2 ▶ next if _unlink($file);
Branch: 1 branch
BranchTrueFalseTotal
if _unlink($file) 2 0 2
149  
150 0 chmod(0777, $file);
151  
152 0 ▶ next if _unlink($file);
Branch: 1 branch
BranchTrueFalseTotal
if _unlink($file) 0 0 0
153  
154 0 require Carp;
155 0 Carp::carp("Cannot delete $file: $!");
156 }
157 }
158  
159 sub _unlink {
160 2 ▶ my $files_unlinked = 0;
Subroutine _unlink: called CC=4 SCAR=13.9
161 2 foreach my $file (@_) {
162 2 my $delete_count = 0;
163 2 ▶ $delete_count++ while unlink $file;
Branch: 1 branch
BranchTrueFalseTotal
unlink $file and do { ++$delete_count } 2 2 4
164 2 ▶ $files_unlinked++ if $delete_count;
Branch: 1 branch
BranchTrueFalseTotal
if $delete_count 2 0 2
165 }
166 2 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 4 ▶ my $t = time;
Subroutine touch: called CC=3 SCAR=11.0
Pod documented
180 4 expand_wildcards();
181 4 foreach my $file (@ARGV) {
182 4 ▶ open(FILE,">>$file") || die "Cannot write $file:$!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">>$file" or die "Cannot write ${file}:$!" 0 4 4
183 4 close(FILE);
184 4 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 2 ▶ expand_wildcards();
Subroutine mv: called CC=5 SCAR=16.3
Pod documented
202 2 my @src = @ARGV;
203 2 my $dst = pop @src;
204  
205 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
206 1 require Carp;
207 1 Carp::croak("Too many arguments");
208 }
209  
210 1 require File::Copy;
211 1 my $nok = 0;
212 1 foreach my $src (@src) {
213 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
214 }
215 1 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 3 ▶ expand_wildcards();
Subroutine cp: called CC=6 SCAR=18.3
Pod documented
232 3 my @src = @ARGV;
233 3 my $dst = pop @src;
234  
235 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
236 1 require Carp;
237 1 Carp::croak("Too many arguments");
238 }
239  
240 2 require File::Copy;
241 2 my $nok = 0;
242 2 foreach my $src (@src) {
243 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
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 2 ▶ utime(time, time, $dst) if $Is_Win32;
Branch: 1 branch
BranchTrueFalseTotal
if $Is_Win32 0 2 2
248 }
249 2 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 7 ▶ local @ARGV = @ARGV;
Subroutine chmod: called CC=6 SCAR=27.3
Pod documented
262 7 my $mode = shift(@ARGV);
263 7 expand_wildcards();
264  
265 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
266 0 require File::Spec;
267 0 foreach my $idx (0..$#ARGV) {
268 0 my $path = $ARGV[$idx];
269 0 ▶ next unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 0 0
270  
271 # chmod 0777, [.foo.bar] doesn't work on VMS, you have to do
272 # chmod 0777, [.foo]bar.dir
273 0 my @dirs = File::Spec->splitdir( $path );
274 0 $dirs[-1] .= '.dir';
275 0 $path = File::Spec->catfile(@dirs);
276  
277 0 $ARGV[$idx] = $path;
278 }
279 }
280  
281 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
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 3 ▶ expand_wildcards();
Subroutine mkpath: called CC=1 SCAR=0.0
Pod documented
295 3 require File::Path;
296 3 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 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
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 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
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 1 ▶ require File::Find;
Subroutine dos2unix: called CC=1 SCAR=0.1
Pod documented
337 File::Find::find(sub {
338 3 ▶ return if -d;
Subroutine __ANON__: called CC=8 SCAR=21.7
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 1 2 3
339 2 ▶ return unless -w _;
Branch: 1 branch
BranchTrueFalseTotal
unless -w _ 0 2 2
340 2 ▶ return unless -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -r _ 0 2 2
341 2 ▶ return if -B _;
Branch: 1 branch
BranchTrueFalseTotal
if -B _ 1 1 2
342  
343 1 local $\;
344  
345 1 my $orig = $_;
346 1 my $temp = '.dos2unix_tmp';
347 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
348 open TEMP, ">$temp" or
349 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
350 1 binmode ORIG; binmode TEMP;
351 1 ▶ while (my $line = <ORIG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline ORIG)) 2 1 3
352 2 $line =~ s/\015\012/\012/g;
353 2 print TEMP $line;
354 }
355 1 close ORIG;
356 1 close TEMP;
357 1 rename $temp, $orig;
358  
359 1 }, @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  
« blib/lib/ExtUtils/MM_OS2.pm ↑ Summary lib/ExtUtils/MM.pm »