Commit | Line | Data |
68dc0745 |
1 | package ExtUtils::Command; |
17f410f9 |
2 | |
57b1a898 |
3 | use 5.00503; |
68dc0745 |
4 | use strict; |
3fe9a6f1 |
5 | use Carp; |
68dc0745 |
6 | use File::Copy; |
7 | use File::Compare; |
8 | use File::Basename; |
9 | use File::Path qw(rmtree); |
10 | require Exporter; |
57b1a898 |
11 | use 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 | |
18 | ExtUtils::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 |
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. |
68dc0745 |
46 | |
47 | =over 4 |
48 | |
3fe9a6f1 |
49 | =cut |
50 | |
51 | sub expand_wildcards |
52 | { |
53 | @ARGV = map(/[\*\?]/ ? glob($_) : $_,@ARGV); |
54 | } |
55 | |
68dc0745 |
56 | =item cat |
57 | |
3fe9a6f1 |
58 | Concatenates all files mentioned on command line to STDOUT. |
68dc0745 |
59 | |
60 | =cut |
61 | |
62 | sub cat () |
63 | { |
3fe9a6f1 |
64 | expand_wildcards(); |
68dc0745 |
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 | |
e38fdfdb |
82 | =item rm_rf files.... |
68dc0745 |
83 | |
84 | Removes directories - recursively (even if readonly) |
85 | |
86 | =cut |
87 | |
88 | sub rm_rf |
89 | { |
57b1a898 |
90 | expand_wildcards(); |
91 | rmtree([grep -e $_,@ARGV],0,0); |
68dc0745 |
92 | } |
93 | |
94 | =item rm_f files.... |
95 | |
96 | Removes files (even if readonly) |
97 | |
98 | =cut |
99 | |
100 | sub 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 | |
115 | Makes files exist, with current timestamp |
116 | |
117 | =cut |
118 | |
119 | sub 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 | |
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); |
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 | |
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); |
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 | |
172 | Sets UNIX like permissions 'mode' on all the files. |
173 | |
174 | =cut |
175 | |
176 | sub 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 | |
185 | Creates directory, including any parent directories. |
186 | |
187 | =cut |
188 | |
189 | sub mkpath |
190 | { |
57b1a898 |
191 | expand_wildcards(); |
192 | File::Path::mkpath([@ARGV],0,0777); |
68dc0745 |
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 | |
5b0d9cbe |
206 | |
68dc0745 |
207 | 1; |
208 | __END__ |
209 | |
210 | =back |
211 | |
212 | =head1 BUGS |
213 | |
68dc0745 |
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 | |