1 package ExtUtils::Command;
6 use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
8 @EXPORT = qw(cp rm_f rm_rf mv cat eqtime mkpath touch test_f test_d chmod
13 my $Is_VMS = $^O eq 'VMS';
14 my $Is_VMS_mode = $Is_VMS;
15 my $Is_VMS_noefs = $Is_VMS;
16 my $Is_Win32 = $^O eq 'MSWin32';
23 if (eval { local $SIG{__DIE__};
25 pop @INC if $INC[-1] eq '.';
26 require VMS::Feature; }) {
27 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
28 $vms_efs = VMS::Feature::current("efs_charset");
29 $vms_case = VMS::Feature::current("efs_case_preserve");
31 my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
32 my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
33 my $efs_case = $ENV{'DECC$EFS_CASE_PRESERVE'} || '';
34 $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
35 $vms_efs = $efs_charset =~ /^[ET1]/i;
36 $vms_case = $efs_case =~ /^[ET1]/i;
38 $Is_VMS_mode = 0 if $vms_unix_rpt;
39 $Is_VMS_noefs = 0 if ($vms_efs);
45 ExtUtils::Command - utilities to replace common UNIX commands in Makefiles etc.
49 perl -MExtUtils::Command -e cat files... > destination
50 perl -MExtUtils::Command -e mv source... destination
51 perl -MExtUtils::Command -e cp source... destination
52 perl -MExtUtils::Command -e touch files...
53 perl -MExtUtils::Command -e rm_f files...
54 perl -MExtUtils::Command -e rm_rf directories...
55 perl -MExtUtils::Command -e mkpath directories...
56 perl -MExtUtils::Command -e eqtime source destination
57 perl -MExtUtils::Command -e test_f file
58 perl -MExtUtils::Command -e test_d directory
59 perl -MExtUtils::Command -e chmod mode files...
64 The module is used to replace common UNIX commands. In all cases the
65 functions work from @ARGV rather than taking arguments. This makes
66 them easier to deal with in Makefiles. Call them like this:
68 perl -MExtUtils::Command -e some_command some files to work on
72 perl -MExtUtils::Command -e 'some_command qw(some files to work on)'
74 For that use L<Shell::Command>.
76 Filenames with * and ? will be glob expanded.
85 # VMS uses % instead of ? to mean "one character"
86 my $wild_regex = $Is_VMS ? '*%' : '*?';
89 @ARGV = map(/[$wild_regex]/o ? glob($_) : $_,@ARGV);
97 Concatenates all files mentioned on command line to STDOUT.
109 eqtime source destination
111 Sets modified time of destination to that of source.
117 my ($src,$dst) = @ARGV;
118 local @ARGV = ($dst); touch(); # in case $dst doesn't exist
119 utime((stat($src))[8,9],$dst);
124 rm_rf files or directories ...
126 Removes files and directories - recursively (even if readonly)
134 File::Path::rmtree([grep -e $_,@ARGV],0,0);
141 Removes files (even if readonly)
148 foreach my $file (@ARGV) {
149 next unless -f $file;
151 next if _unlink($file);
155 next if _unlink($file);
158 Carp::carp("Cannot delete $file: $!");
163 my $files_unlinked = 0;
164 foreach my $file (@_) {
165 my $delete_count = 0;
166 $delete_count++ while unlink $file;
167 $files_unlinked++ if $delete_count;
169 return $files_unlinked;
177 Makes files exist, with current timestamp
184 foreach my $file (@ARGV) {
185 open(FILE,">>$file") || die "Cannot write $file:$!";
193 mv source_file destination_file
194 mv source_file source_file destination_dir
196 Moves source to destination. Multiple sources are allowed if
197 destination is an existing directory.
199 Returns true if all moves succeeded, false otherwise.
208 if (@src > 1 && ! -d $dst) {
210 Carp::croak("Too many arguments");
215 foreach my $src (@src) {
216 $nok ||= !File::Copy::move($src,$dst);
223 cp source_file destination_file
224 cp source_file source_file destination_dir
226 Copies sources to the destination. Multiple sources are allowed if
227 destination is an existing directory.
229 Returns true if all copies succeeded, false otherwise.
238 if (@src > 1 && ! -d $dst) {
240 Carp::croak("Too many arguments");
245 foreach my $src (@src) {
246 $nok ||= !File::Copy::copy($src,$dst);
248 # Win32 does not update the mod time of a copied file, just the
249 # created time which make does not look at.
250 utime(time, time, $dst) if $Is_Win32;
259 Sets UNIX like permissions 'mode' on all the files. e.g. 0666
265 my $mode = shift(@ARGV);
268 if( $Is_VMS_mode && $Is_VMS_noefs) {
270 foreach my $idx (0..$#ARGV) {
271 my $path = $ARGV[$idx];
272 next unless -d $path;
274 # chmod 0777, [.foo.bar] doesn't work on VMS, you have to do
275 # chmod 0777, [.foo]bar.dir
276 my @dirs = File::Spec->splitdir( $path );
278 $path = File::Spec->catfile(@dirs);
284 chmod(oct $mode,@ARGV) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
291 Creates directories, including any parent directories.
299 File::Path::mkpath([@ARGV],0,0777);
306 Tests if a file exists. I<Exits> with 0 if it does, 1 if it does not (ie.
307 shell's idea of true and false).
313 exit(-f $ARGV[0] ? 0 : 1);
320 Tests if a directory exists. I<Exits> with 0 if it does, 1 if it does
321 not (ie. shell's idea of true and false).
327 exit(-d $ARGV[0] ? 0 : 1);
332 dos2unix files or dirs ...
334 Converts DOS and OS/2 linefeeds to Unix style recursively.
340 File::Find::find(sub {
349 my $temp = '.dos2unix_tmp';
350 open ORIG, $_ or do { warn "dos2unix can't open $_: $!"; return };
351 open TEMP, ">$temp" or
352 do { warn "dos2unix can't create .dos2unix_tmp: $!"; return };
353 binmode ORIG; binmode TEMP;
354 while (my $line = <ORIG>) {
355 $line =~ s/\015\012/\012/g;
369 Shell::Command which is these same functions but take arguments normally.
374 Nick Ing-Simmons C<ni-s@cpan.org>
376 Maintained by Michael G Schwern C<schwern@pobox.com> within the
377 ExtUtils-MakeMaker package and, as a separate CPAN package, by
378 Randy Kobes C<r.kobes@uwinnipeg.ca>.