Integrate mainline
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / Command.pm
CommitLineData
68dc0745 1package ExtUtils::Command;
17f410f9 2
57b1a898 3use 5.00503;
68dc0745 4use strict;
3fe9a6f1 5use Carp;
68dc0745 6use File::Copy;
7use File::Compare;
8use File::Basename;
9use File::Path qw(rmtree);
10require Exporter;
57b1a898 11use vars qw(@ISA @EXPORT $VERSION);
68dc0745 12@ISA = qw(Exporter);
13@EXPORT = qw(cp rm_f rm_rf mv cat eqtime mkpath touch test_f);
f6d6199c 14$VERSION = '1.03_01';
68dc0745 15
16=head1 NAME
17
18ExtUtils::Command - utilities to replace common UNIX commands in Makefiles etc.
19
dc848c6f 20=head1 SYNOPSIS
68dc0745 21
84902520 22 perl -MExtUtils::Command -e cat files... > destination
23 perl -MExtUtils::Command -e mv source... destination
24 perl -MExtUtils::Command -e cp source... destination
25 perl -MExtUtils::Command -e touch files...
26 perl -MExtUtils::Command -e rm_f file...
27 perl -MExtUtils::Command -e rm_rf directories...
28 perl -MExtUtils::Command -e mkpath directories...
29 perl -MExtUtils::Command -e eqtime source destination
30 perl -MExtUtils::Command -e chmod mode files...
31 perl -MExtUtils::Command -e test_f file
68dc0745 32
33=head1 DESCRIPTION
34
57b1a898 35The module is used to replace common UNIX commands. In all cases the
36functions work from @ARGV rather than taking arguments. This makes
37them easier to deal with in Makefiles.
38
39 perl -MExtUtils::Command -e some_command some files to work on
40
41I<NOT>
42
43 perl -MExtUtils::Command -e 'some_command qw(some files to work on)'
44
45Filenames with * and ? will be glob expanded.
68dc0745 46
47=over 4
48
3fe9a6f1 49=cut
50
51sub expand_wildcards
52{
53 @ARGV = map(/[\*\?]/ ? glob($_) : $_,@ARGV);
54}
55
68dc0745 56=item cat
57
3fe9a6f1 58Concatenates all files mentioned on command line to STDOUT.
68dc0745 59
60=cut
61
62sub cat ()
63{
3fe9a6f1 64 expand_wildcards();
68dc0745 65 print while (<>);
66}
67
68=item eqtime src dst
69
70Sets modified time of dst to that of src
71
72=cut
73
74sub eqtime
75{
76 my ($src,$dst) = @ARGV;
77 open(F,">$dst");
78 close(F);
79 utime((stat($src))[8,9],$dst);
80}
81
e38fdfdb 82=item rm_rf files....
68dc0745 83
84Removes directories - recursively (even if readonly)
85
86=cut
87
88sub rm_rf
89{
57b1a898 90 expand_wildcards();
91 rmtree([grep -e $_,@ARGV],0,0);
68dc0745 92}
93
94=item rm_f files....
95
96Removes files (even if readonly)
97
98=cut
99
100sub rm_f
101{
57b1a898 102 expand_wildcards();
103 foreach (@ARGV)
68dc0745 104 {
3fe9a6f1 105 next unless -f $_;
106 next if unlink($_);
107 chmod(0777,$_);
108 next if unlink($_);
109 carp "Cannot delete $_:$!";
68dc0745 110 }
111}
112
113=item touch files ...
114
115Makes files exist, with current timestamp
116
117=cut
118
119sub touch
120{
5b0d9cbe 121 my $t = time;
fbac1b85 122 expand_wildcards();
68dc0745 123 while (@ARGV)
124 {
125 my $file = shift(@ARGV);
126 open(FILE,">>$file") || die "Cannot write $file:$!";
127 close(FILE);
5b0d9cbe 128 utime($t,$t,$file);
68dc0745 129 }
130}
131
132=item mv source... destination
133
134Moves source to destination.
135Multiple sources are allowed if destination is an existing directory.
136
137=cut
138
139sub mv
140{
141 my $dst = pop(@ARGV);
3fe9a6f1 142 expand_wildcards();
143 croak("Too many arguments") if (@ARGV > 1 && ! -d $dst);
144 while (@ARGV)
68dc0745 145 {
3fe9a6f1 146 my $src = shift(@ARGV);
147 move($src,$dst);
68dc0745 148 }
149}
150
151=item cp source... destination
152
153Copies source to destination.
154Multiple sources are allowed if destination is an existing directory.
155
156=cut
157
158sub cp
159{
160 my $dst = pop(@ARGV);
3fe9a6f1 161 expand_wildcards();
162 croak("Too many arguments") if (@ARGV > 1 && ! -d $dst);
163 while (@ARGV)
68dc0745 164 {
3fe9a6f1 165 my $src = shift(@ARGV);
166 copy($src,$dst);
68dc0745 167 }
168}
169
170=item chmod mode files...
171
172Sets UNIX like permissions 'mode' on all the files.
173
174=cut
175
176sub chmod
177{
3fe9a6f1 178 my $mode = shift(@ARGV);
57b1a898 179 expand_wildcards();
180 chmod($mode,@ARGV) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
68dc0745 181}
182
183=item mkpath directory...
184
185Creates directory, including any parent directories.
186
187=cut
188
189sub mkpath
190{
57b1a898 191 expand_wildcards();
192 File::Path::mkpath([@ARGV],0,0777);
68dc0745 193}
194
195=item test_f file
196
197Tests if a file exists
198
199=cut
200
201sub test_f
202{
203 exit !-f shift(@ARGV);
204}
205
5b0d9cbe 206
68dc0745 2071;
208__END__
209
210=back
211
212=head1 BUGS
213
68dc0745 214Should probably be Auto/Self loaded.
215
216=head1 SEE ALSO
217
218ExtUtils::MakeMaker, ExtUtils::MM_Unix, ExtUtils::MM_Win32
219
220=head1 AUTHOR
221
222Nick Ing-Simmons <F<nick@ni-s.u-net.com>>.
223
224=cut
225