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 For that use L<Shell::Command>.
51 Filenames with * and ? will be glob expanded.
57 # VMS uses % instead of ? to mean "one character"
58 my $wild_regex = $Is_VMS ? '*%' : '*?';
61 @ARGV = map(/[$wild_regex]/o ? glob($_) : $_,@ARGV);
69 Concatenates all files mentioned on command line to STDOUT.
81 eqtime source destination
83 Sets modified time of destination to that of source.
89 my ($src,$dst) = @ARGV;
90 local @ARGV = ($dst); touch(); # in case $dst doesn't exist
91 utime((stat($src))[8,9],$dst);
96 rm_rf files or directories ...
98 Removes files and directories - recursively (even if readonly)
105 rmtree([grep -e $_,@ARGV],0,0);
112 Removes files (even if readonly)
119 foreach my $file (@ARGV) {
120 next unless -f $file;
122 next if _unlink($file);
126 next if _unlink($file);
128 carp "Cannot delete $file: $!";
133 my $files_unlinked = 0;
134 foreach my $file (@_) {
135 my $delete_count = 0;
136 $delete_count++ while unlink $file;
137 $files_unlinked++ if $delete_count;
139 return $files_unlinked;
147 Makes files exist, with current timestamp
154 foreach my $file (@ARGV) {
155 open(FILE,">>$file") || die "Cannot write $file:$!";
163 mv source_file destination_file
164 mv source_file source_file destination_dir
166 Moves source to destination. Multiple sources are allowed if
167 destination is an existing directory.
169 Returns true if all moves succeeded, false otherwise.
178 croak("Too many arguments") if (@src > 1 && ! -d $dst);
181 foreach my $src (@src) {
182 $nok ||= !move($src,$dst);
189 cp source_file destination_file
190 cp source_file source_file destination_dir
192 Copies sources to the destination. Multiple sources are allowed if
193 destination is an existing directory.
195 Returns true if all copies succeeded, false otherwise.
204 croak("Too many arguments") if (@src > 1 && ! -d $dst);
207 foreach my $src (@src) {
208 $nok ||= !copy($src,$dst);
217 Sets UNIX like permissions 'mode' on all the files. e.g. 0666
223 my $mode = shift(@ARGV);
227 foreach my $idx (0..$#ARGV) {
228 my $path = $ARGV[$idx];
229 next unless -d $path;
231 # chmod 0777, [.foo.bar] doesn't work on VMS, you have to do
232 # chmod 0777, [.foo]bar.dir
233 my @dirs = File::Spec->splitdir( $path );
235 $path = File::Spec->catfile(@dirs);
241 chmod(oct $mode,@ARGV) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
248 Creates directories, including any parent directories.
255 File::Path::mkpath([@ARGV],0,0777);
262 Tests if a file exists
273 dos2unix files or dirs ...
275 Converts DOS and OS/2 linefeeds to Unix style recursively.
281 File::Find::find(sub {
290 my $temp = '.dos2unix_tmp';
291 open ORIG, $_ or do { warn "dos2unix can't open $_: $!"; return };
292 open TEMP, ">$temp" or
293 do { warn "dos2unix can't create .dos2unix_tmp: $!"; return };
294 while (my $line = <ORIG>) {
295 $line =~ s/\015\012/\012/g;
309 Shell::Command which is these same functions but take arguments normally.
314 Nick Ing-Simmons C<ni-s@cpan.org>
316 Currently maintained by Michael G Schwern C<schwern@pobox.com>.