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