1 package ExtUtils::Command;
9 use File::Path qw(rmtree);
11 use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
13 @EXPORT = qw(cp rm_f rm_rf mv cat eqtime mkpath touch test_f test_d chmod
17 my $Is_VMS = $^O eq 'VMS';
18 my $Is_Win32 = $^O eq 'MSWin32';
23 ExtUtils::Command - utilities to replace common UNIX commands in Makefiles etc.
27 perl -MExtUtils::Command -e cat files... > destination
28 perl -MExtUtils::Command -e mv source... destination
29 perl -MExtUtils::Command -e cp source... destination
30 perl -MExtUtils::Command -e touch files...
31 perl -MExtUtils::Command -e rm_f files...
32 perl -MExtUtils::Command -e rm_rf directories...
33 perl -MExtUtils::Command -e mkpath directories...
34 perl -MExtUtils::Command -e eqtime source destination
35 perl -MExtUtils::Command -e test_f file
36 perl -MExtUtils::Command -e test_d directory
37 perl -MExtUtils::Command -e chmod mode files...
42 The module is used to replace common UNIX commands. In all cases the
43 functions work from @ARGV rather than taking arguments. This makes
44 them easier to deal with in Makefiles. Call them like this:
46 perl -MExtUtils::Command -e some_command some files to work on
50 perl -MExtUtils::Command -e 'some_command qw(some files to work on)'
52 For that use L<Shell::Command>.
54 Filenames with * and ? will be glob expanded.
63 # VMS uses % instead of ? to mean "one character"
64 my $wild_regex = $Is_VMS ? '*%' : '*?';
67 @ARGV = map(/[$wild_regex]/o ? glob($_) : $_,@ARGV);
75 Concatenates all files mentioned on command line to STDOUT.
87 eqtime source destination
89 Sets modified time of destination to that of source.
95 my ($src,$dst) = @ARGV;
96 local @ARGV = ($dst); touch(); # in case $dst doesn't exist
97 utime((stat($src))[8,9],$dst);
102 rm_rf files or directories ...
104 Removes files and directories - recursively (even if readonly)
111 rmtree([grep -e $_,@ARGV],0,0);
118 Removes files (even if readonly)
125 foreach my $file (@ARGV) {
126 next unless -f $file;
128 next if _unlink($file);
132 next if _unlink($file);
134 carp "Cannot delete $file: $!";
139 my $files_unlinked = 0;
140 foreach my $file (@_) {
141 my $delete_count = 0;
142 $delete_count++ while unlink $file;
143 $files_unlinked++ if $delete_count;
145 return $files_unlinked;
153 Makes files exist, with current timestamp
160 foreach my $file (@ARGV) {
161 open(FILE,">>$file") || die "Cannot write $file:$!";
169 mv source_file destination_file
170 mv source_file source_file destination_dir
172 Moves source to destination. Multiple sources are allowed if
173 destination is an existing directory.
175 Returns true if all moves succeeded, false otherwise.
184 croak("Too many arguments") if (@src > 1 && ! -d $dst);
187 foreach my $src (@src) {
188 $nok ||= !move($src,$dst);
195 cp source_file destination_file
196 cp source_file source_file destination_dir
198 Copies sources to the destination. Multiple sources are allowed if
199 destination is an existing directory.
201 Returns true if all copies succeeded, false otherwise.
210 croak("Too many arguments") if (@src > 1 && ! -d $dst);
213 foreach my $src (@src) {
214 $nok ||= !copy($src,$dst);
216 # Win32 does not update the mod time of a copied file, just the
217 # created time which make does not look at.
218 utime(time, time, $dst) if $Is_Win32;
227 Sets UNIX like permissions 'mode' on all the files. e.g. 0666
233 my $mode = shift(@ARGV);
237 foreach my $idx (0..$#ARGV) {
238 my $path = $ARGV[$idx];
239 next unless -d $path;
241 # chmod 0777, [.foo.bar] doesn't work on VMS, you have to do
242 # chmod 0777, [.foo]bar.dir
243 my @dirs = File::Spec->splitdir( $path );
245 $path = File::Spec->catfile(@dirs);
251 chmod(oct $mode,@ARGV) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
258 Creates directories, including any parent directories.
265 File::Path::mkpath([@ARGV],0,0777);
272 Tests if a file exists. I<Exits> with 0 if it does, 1 if it does not (ie.
273 shell's idea of true and false).
279 exit(-f $ARGV[0] ? 0 : 1);
286 Tests if a directory exists. I<Exits> with 0 if it does, 1 if it does
287 not (ie. shell's idea of true and false).
293 exit(-d $ARGV[0] ? 0 : 1);
298 dos2unix files or dirs ...
300 Converts DOS and OS/2 linefeeds to Unix style recursively.
306 File::Find::find(sub {
315 my $temp = '.dos2unix_tmp';
316 open ORIG, $_ or do { warn "dos2unix can't open $_: $!"; return };
317 open TEMP, ">$temp" or
318 do { warn "dos2unix can't create .dos2unix_tmp: $!"; return };
319 while (my $line = <ORIG>) {
320 $line =~ s/\015\012/\012/g;
334 Shell::Command which is these same functions but take arguments normally.
339 Nick Ing-Simmons C<ni-s@cpan.org>
341 Maintained by Michael G Schwern C<schwern@pobox.com> within the
342 ExtUtils-MakeMaker package and, as a separate CPAN package, by
343 Randy Kobes C<r.kobes@uwinnipeg.ca>.