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';
21 ExtUtils::Command - utilities to replace common UNIX commands in Makefiles etc.
25 perl -MExtUtils::Command -e cat files... > destination
26 perl -MExtUtils::Command -e mv source... destination
27 perl -MExtUtils::Command -e cp source... destination
28 perl -MExtUtils::Command -e touch files...
29 perl -MExtUtils::Command -e rm_f files...
30 perl -MExtUtils::Command -e rm_rf directories...
31 perl -MExtUtils::Command -e mkpath directories...
32 perl -MExtUtils::Command -e eqtime source destination
33 perl -MExtUtils::Command -e test_f file
34 perl -MExtUtils::Command -e test_d directory
35 perl -MExtUtils::Command -e chmod mode files...
40 The module is used to replace common UNIX commands. In all cases the
41 functions work from @ARGV rather than taking arguments. This makes
42 them easier to deal with in Makefiles.
44 perl -MExtUtils::Command -e some_command some files to work on
48 perl -MExtUtils::Command -e 'some_command qw(some files to work on)'
50 Filenames with * and ? will be glob expanded.
56 # VMS uses % instead of ? to mean "one character"
57 my $wild_regex = $Is_VMS ? '*%' : '*?';
60 @ARGV = map(/[$wild_regex]/o ? glob($_) : $_,@ARGV);
66 Concatenates all files mentioned on command line to STDOUT.
78 Sets modified time of dst to that of src
84 my ($src,$dst) = @ARGV;
85 local @ARGV = ($dst); touch(); # in case $dst doesn't exist
86 utime((stat($src))[8,9],$dst);
91 Removes directories - recursively (even if readonly)
98 rmtree([grep -e $_,@ARGV],0,0);
103 Removes files (even if readonly)
110 foreach my $file (@ARGV) {
111 next unless -f $file;
113 next if _unlink($file);
117 next if _unlink($file);
119 carp "Cannot delete $file: $!";
124 my $files_unlinked = 0;
125 foreach my $file (@_) {
126 my $delete_count = 0;
127 $delete_count++ while unlink $file;
128 $files_unlinked++ if $delete_count;
130 return $files_unlinked;
134 =item touch files ...
136 Makes files exist, with current timestamp
143 foreach my $file (@ARGV) {
144 open(FILE,">>$file") || die "Cannot write $file:$!";
150 =item mv source... destination
152 Moves source to destination. Multiple sources are allowed if
153 destination is an existing directory.
155 Returns true if all moves succeeded, false otherwise.
164 croak("Too many arguments") if (@src > 1 && ! -d $dst);
167 foreach my $src (@src) {
168 $nok ||= !move($src,$dst);
173 =item cp source... destination
175 Copies source to destination. Multiple sources are allowed if
176 destination is an existing directory.
178 Returns true if all copies succeeded, false otherwise.
187 croak("Too many arguments") if (@src > 1 && ! -d $dst);
190 foreach my $src (@src) {
191 $nok ||= !copy($src,$dst);
196 =item chmod mode files...
198 Sets UNIX like permissions 'mode' on all the files. e.g. 0666
204 my $mode = shift(@ARGV);
208 foreach my $idx (0..$#ARGV) {
209 my $path = $ARGV[$idx];
210 next unless -d $path;
212 # chmod 0777, [.foo.bar] doesn't work on VMS, you have to do
213 # chmod 0777, [.foo]bar.dir
214 my @dirs = File::Spec->splitdir( $path );
216 $path = File::Spec->catfile(@dirs);
222 chmod(oct $mode,@ARGV) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
225 =item mkpath directory...
227 Creates directory, including any parent directories.
234 File::Path::mkpath([@ARGV],0,0777);
239 Tests if a file exists
248 =item test_d directory
250 Tests if a directory exists
261 Converts DOS and OS/2 linefeeds to Unix style recursively.
267 File::Find::find(sub {
276 my $temp = '.dos2unix_tmp';
277 open ORIG, $_ or do { warn "dos2unix can't open $_: $!"; return };
278 open TEMP, ">$temp" or
279 do { warn "dos2unix can't create .dos2unix_tmp: $!"; return };
280 while (my $line = <ORIG>) {
281 $line =~ s/\015\012/\012/g;
295 Should probably be Auto/Self loaded.
299 ExtUtils::MakeMaker, ExtUtils::MM_Unix, ExtUtils::MM_Win32
303 Nick Ing-Simmons C<ni-s@cpan.org>
305 Maintained by Michael G Schwern C<schwern@pobox.com> within the
306 ExtUtils-MakeMaker package and, as a separate CPAN package, by
307 Randy Kobes C<r.kobes@uwinnipeg.ca>.