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