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