This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to MakeMaker 6.20.
[perl5.git] / lib / ExtUtils / Command.pm
CommitLineData
68dc0745 1package ExtUtils::Command;
17f410f9 2
57b1a898 3use 5.00503;
68dc0745 4use strict;
3fe9a6f1 5use Carp;
68dc0745
PP
6use File::Copy;
7use File::Compare;
8use File::Basename;
9use File::Path qw(rmtree);
10require Exporter;
a7d1454b
RGS
11use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
12@ISA = qw(Exporter);
13@EXPORT = qw(cp rm_f rm_rf mv cat eqtime mkpath touch test_f chmod
14 dos2unix);
dd0810f9 15$VERSION = '1.07';
68dc0745 16
a67d7a01
MS
17my $Is_VMS = $^O eq 'VMS';
18
68dc0745
PP
19=head1 NAME
20
21ExtUtils::Command - utilities to replace common UNIX commands in Makefiles etc.
22
dc848c6f 23=head1 SYNOPSIS
68dc0745 24
479d2113
MS
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
a7d1454b
RGS
34 perl -MExtUtils::Command -e chmod mode files...
35 ...
68dc0745
PP
36
37=head1 DESCRIPTION
38
57b1a898
MS
39The module is used to replace common UNIX commands. In all cases the
40functions work from @ARGV rather than taking arguments. This makes
41them easier to deal with in Makefiles.
42
43 perl -MExtUtils::Command -e some_command some files to work on
44
45I<NOT>
46
47 perl -MExtUtils::Command -e 'some_command qw(some files to work on)'
48
49Filenames with * and ? will be glob expanded.
68dc0745
PP
50
51=over 4
52
3fe9a6f1
PP
53=cut
54
a67d7a01
MS
55# VMS uses % instead of ? to mean "one character"
56my $wild_regex = $Is_VMS ? '*%' : '*?';
3fe9a6f1
PP
57sub expand_wildcards
58{
a67d7a01 59 @ARGV = map(/[$wild_regex]/o ? glob($_) : $_,@ARGV);
3fe9a6f1
PP
60}
61
479d2113 62
68dc0745
PP
63=item cat
64
3fe9a6f1 65Concatenates all files mentioned on command line to STDOUT.
68dc0745
PP
66
67=cut
68
69sub cat ()
70{
3fe9a6f1 71 expand_wildcards();
68dc0745
PP
72 print while (<>);
73}
74
75=item eqtime src dst
76
77Sets modified time of dst to that of src
78
79=cut
80
81sub eqtime
82{
83 my ($src,$dst) = @ARGV;
479d2113 84 local @ARGV = ($dst); touch(); # in case $dst doesn't exist
68dc0745
PP
85 utime((stat($src))[8,9],$dst);
86}
87
e38fdfdb 88=item rm_rf files....
68dc0745
PP
89
90Removes directories - recursively (even if readonly)
91
92=cut
93
94sub rm_rf
95{
57b1a898
MS
96 expand_wildcards();
97 rmtree([grep -e $_,@ARGV],0,0);
68dc0745
PP
98}
99
100=item rm_f files....
101
102Removes files (even if readonly)
103
104=cut
105
106sub rm_f
107{
57b1a898
MS
108 expand_wildcards();
109 foreach (@ARGV)
68dc0745 110 {
d5d4ec93 111 next unless -f $_;
3fe9a6f1 112 next if unlink($_);
d5d4ec93 113 chmod(0777,$_);
3fe9a6f1
PP
114 next if unlink($_);
115 carp "Cannot delete $_:$!";
68dc0745
PP
116 }
117}
118
119=item touch files ...
120
121Makes files exist, with current timestamp
122
123=cut
124
479d2113
MS
125sub touch {
126 my $t = time;
127 expand_wildcards();
128 foreach my $file (@ARGV) {
129 open(FILE,">>$file") || die "Cannot write $file:$!";
130 close(FILE);
131 utime($t,$t,$file);
132 }
68dc0745
PP
133}
134
135=item mv source... destination
136
a7d1454b
RGS
137Moves source to destination. Multiple sources are allowed if
138destination is an existing directory.
139
140Returns true if all moves succeeded, false otherwise.
68dc0745
PP
141
142=cut
143
479d2113 144sub mv {
479d2113 145 expand_wildcards();
a7d1454b
RGS
146 my @src = @ARGV;
147 my $dst = pop @src;
148
149 croak("Too many arguments") if (@src > 1 && ! -d $dst);
150
151 my $nok = 0;
152 foreach my $src (@src) {
153 $nok ||= !move($src,$dst);
479d2113 154 }
a7d1454b 155 return !$nok;
68dc0745
PP
156}
157
158=item cp source... destination
159
a7d1454b
RGS
160Copies source to destination. Multiple sources are allowed if
161destination is an existing directory.
162
163Returns true if all copies succeeded, false otherwise.
68dc0745 164
d5d4ec93 165=cut
68dc0745 166
479d2113 167sub cp {
479d2113 168 expand_wildcards();
a7d1454b
RGS
169 my @src = @ARGV;
170 my $dst = pop @src;
171
172 croak("Too many arguments") if (@src > 1 && ! -d $dst);
173
174 my $nok = 0;
175 foreach my $src (@src) {
176 $nok ||= !copy($src,$dst);
479d2113 177 }
a7d1454b 178 return $nok;
68dc0745
PP
179}
180
181=item chmod mode files...
182
479d2113 183Sets UNIX like permissions 'mode' on all the files. e.g. 0666
68dc0745
PP
184
185=cut
186
479d2113 187sub chmod {
a7d1454b 188 local @ARGV = @ARGV;
479d2113
MS
189 my $mode = shift(@ARGV);
190 expand_wildcards();
191 chmod(oct $mode,@ARGV) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
68dc0745
PP
192}
193
194=item mkpath directory...
195
196Creates directory, including any parent directories.
197
198=cut
199
200sub mkpath
201{
57b1a898
MS
202 expand_wildcards();
203 File::Path::mkpath([@ARGV],0,0777);
68dc0745
PP
204}
205
206=item test_f file
207
208Tests if a file exists
209
210=cut
211
212sub test_f
213{
a7d1454b 214 exit !-f $ARGV[0];
68dc0745
PP
215}
216
a7d1454b
RGS
217=item dos2unix
218
219Converts DOS and OS/2 linefeeds to Unix style recursively.
5b0d9cbe 220
a7d1454b
RGS
221=cut
222
223sub dos2unix {
224 require File::Find;
225 File::Find::find(sub {
dd0810f9 226 return if -d;
a7d1454b 227 return unless -w _;
dd0810f9 228 return unless -r _;
a7d1454b
RGS
229 return if -B _;
230
a7d1454b
RGS
231 local $\;
232
dd0810f9
RGS
233 my $orig = $_;
234 my $temp = '.dos2unix_tmp';
235 open ORIG, $_ or do { warn "dos2unix can't open $_: $!"; return };
236 open TEMP, ">$temp" or
237 do { warn "dos2unix can't create .dos2unix_tmp: $!"; return };
238 while (my $line = <ORIG>) {
239 $line =~ s/\015\012/\012/g;
240 print TEMP $line;
a7d1454b 241 }
dd0810f9
RGS
242 close ORIG;
243 close TEMP;
244 rename $temp, $orig;
a7d1454b
RGS
245
246 }, @ARGV);
247}
68dc0745
PP
248
249=back
250
251=head1 BUGS
252
68dc0745
PP
253Should probably be Auto/Self loaded.
254
255=head1 SEE ALSO
256
257ExtUtils::MakeMaker, ExtUtils::MM_Unix, ExtUtils::MM_Win32
258
259=head1 AUTHOR
260
a7d1454b
RGS
261Nick Ing-Simmons C<ni-s@cpan.org>
262
263Currently maintained by Michael G Schwern C<schwern@pobox.com>.
68dc0745
PP
264
265=cut
266