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 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 chmod mode files...
39 The module is used to replace common UNIX commands. In all cases the
40 functions work from @ARGV rather than taking arguments. This makes
41 them easier to deal with in Makefiles.
43 perl -MExtUtils::Command -e some_command some files to work on
47 perl -MExtUtils::Command -e 'some_command qw(some files to work on)'
49 Filenames with * and ? will be glob expanded.
55 # VMS uses % instead of ? to mean "one character"
56 my $wild_regex = $Is_VMS ? '*%' : '*?';
59 @ARGV = map(/[$wild_regex]/o ? glob($_) : $_,@ARGV);
65 Concatenates all files mentioned on command line to STDOUT.
77 Sets modified time of dst to that of src
83 my ($src,$dst) = @ARGV;
84 local @ARGV = ($dst); touch(); # in case $dst doesn't exist
85 utime((stat($src))[8,9],$dst);
90 Removes directories - recursively (even if readonly)
97 rmtree([grep -e $_,@ARGV],0,0);
102 Removes files (even if readonly)
109 foreach my $file (@ARGV) {
110 next unless -f $file;
112 next if _unlink($file);
116 next if _unlink($file);
118 carp "Cannot delete $file: $!";
123 my $files_unlinked = 0;
124 foreach my $file (@_) {
125 my $delete_count = 0;
126 $delete_count++ while unlink $file;
127 $files_unlinked++ if $delete_count;
129 return $files_unlinked;
133 =item touch files ...
135 Makes files exist, with current timestamp
142 foreach my $file (@ARGV) {
143 open(FILE,">>$file") || die "Cannot write $file:$!";
149 =item mv source... destination
151 Moves source to destination. Multiple sources are allowed if
152 destination is an existing directory.
154 Returns true if all moves succeeded, false otherwise.
163 croak("Too many arguments") if (@src > 1 && ! -d $dst);
166 foreach my $src (@src) {
167 $nok ||= !move($src,$dst);
172 =item cp source... destination
174 Copies source to destination. Multiple sources are allowed if
175 destination is an existing directory.
177 Returns true if all copies succeeded, false otherwise.
186 croak("Too many arguments") if (@src > 1 && ! -d $dst);
189 foreach my $src (@src) {
190 $nok ||= !copy($src,$dst);
195 =item chmod mode files...
197 Sets UNIX like permissions 'mode' on all the files. e.g. 0666
203 my $mode = shift(@ARGV);
207 foreach my $idx (0..$#ARGV) {
208 my $path = $ARGV[$idx];
209 next unless -d $path;
211 # chmod 0777, [.foo.bar] doesn't work on VMS, you have to do
212 # chmod 0777, [.foo]bar.dir
213 my @dirs = File::Spec->splitdir( $path );
215 $path = File::Spec->catfile(@dirs);
221 chmod(oct $mode,@ARGV) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
224 =item mkpath directory...
226 Creates directory, including any parent directories.
233 File::Path::mkpath([@ARGV],0,0777);
238 Tests if a file exists
249 Converts DOS and OS/2 linefeeds to Unix style recursively.
255 File::Find::find(sub {
264 my $temp = '.dos2unix_tmp';
265 open ORIG, $_ or do { warn "dos2unix can't open $_: $!"; return };
266 open TEMP, ">$temp" or
267 do { warn "dos2unix can't create .dos2unix_tmp: $!"; return };
268 while (my $line = <ORIG>) {
269 $line =~ s/\015\012/\012/g;
283 Should probably be Auto/Self loaded.
287 ExtUtils::MakeMaker, ExtUtils::MM_Unix, ExtUtils::MM_Win32
291 Nick Ing-Simmons C<ni-s@cpan.org>
293 Currently maintained by Michael G Schwern C<schwern@pobox.com>.