xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/ExtUtils/Command.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage ExtUtils::Command;
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateuse 5.00503;
4*0Sstevel@tonic-gateuse strict;
5*0Sstevel@tonic-gateuse Carp;
6*0Sstevel@tonic-gateuse File::Copy;
7*0Sstevel@tonic-gateuse File::Compare;
8*0Sstevel@tonic-gateuse File::Basename;
9*0Sstevel@tonic-gateuse File::Path qw(rmtree);
10*0Sstevel@tonic-gaterequire Exporter;
11*0Sstevel@tonic-gateuse vars qw(@ISA @EXPORT $VERSION);
12*0Sstevel@tonic-gate@ISA     = qw(Exporter);
13*0Sstevel@tonic-gate@EXPORT  = qw(cp rm_f rm_rf mv cat eqtime mkpath touch test_f);
14*0Sstevel@tonic-gate$VERSION = '1.05';
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gatemy $Is_VMS = $^O eq 'VMS';
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gate=head1 NAME
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gateExtUtils::Command - utilities to replace common UNIX commands in Makefiles etc.
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gate=head1 SYNOPSIS
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gate  perl -MExtUtils::Command       -e cat files... > destination
25*0Sstevel@tonic-gate  perl -MExtUtils::Command       -e mv source... destination
26*0Sstevel@tonic-gate  perl -MExtUtils::Command       -e cp source... destination
27*0Sstevel@tonic-gate  perl -MExtUtils::Command       -e touch files...
28*0Sstevel@tonic-gate  perl -MExtUtils::Command       -e rm_f files...
29*0Sstevel@tonic-gate  perl -MExtUtils::Command       -e rm_rf directories...
30*0Sstevel@tonic-gate  perl -MExtUtils::Command       -e mkpath directories...
31*0Sstevel@tonic-gate  perl -MExtUtils::Command       -e eqtime source destination
32*0Sstevel@tonic-gate  perl -MExtUtils::Command       -e test_f file
33*0Sstevel@tonic-gate  perl -MExtUtils::Command=chmod -e chmod mode files...
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate=head1 DESCRIPTION
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gateThe module is used to replace common UNIX commands.  In all cases the
38*0Sstevel@tonic-gatefunctions work from @ARGV rather than taking arguments.  This makes
39*0Sstevel@tonic-gatethem easier to deal with in Makefiles.
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate  perl -MExtUtils::Command -e some_command some files to work on
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gateI<NOT>
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate  perl -MExtUtils::Command -e 'some_command qw(some files to work on)'
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gateFilenames with * and ? will be glob expanded.
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate=over 4
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate=cut
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate# VMS uses % instead of ? to mean "one character"
54*0Sstevel@tonic-gatemy $wild_regex = $Is_VMS ? '*%' : '*?';
55*0Sstevel@tonic-gatesub expand_wildcards
56*0Sstevel@tonic-gate{
57*0Sstevel@tonic-gate @ARGV = map(/[$wild_regex]/o ? glob($_) : $_,@ARGV);
58*0Sstevel@tonic-gate}
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate=item cat
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gateConcatenates all files mentioned on command line to STDOUT.
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate=cut 
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gatesub cat ()
68*0Sstevel@tonic-gate{
69*0Sstevel@tonic-gate expand_wildcards();
70*0Sstevel@tonic-gate print while (<>);
71*0Sstevel@tonic-gate}
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate=item eqtime src dst
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gateSets modified time of dst to that of src
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate=cut 
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gatesub eqtime
80*0Sstevel@tonic-gate{
81*0Sstevel@tonic-gate my ($src,$dst) = @ARGV;
82*0Sstevel@tonic-gate local @ARGV = ($dst);  touch();  # in case $dst doesn't exist
83*0Sstevel@tonic-gate utime((stat($src))[8,9],$dst);
84*0Sstevel@tonic-gate}
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate=item rm_rf files....
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gateRemoves directories - recursively (even if readonly)
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate=cut 
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gatesub rm_rf
93*0Sstevel@tonic-gate{
94*0Sstevel@tonic-gate expand_wildcards();
95*0Sstevel@tonic-gate rmtree([grep -e $_,@ARGV],0,0);
96*0Sstevel@tonic-gate}
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate=item rm_f files....
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gateRemoves files (even if readonly)
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate=cut 
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gatesub rm_f
105*0Sstevel@tonic-gate{
106*0Sstevel@tonic-gate expand_wildcards();
107*0Sstevel@tonic-gate foreach (@ARGV)
108*0Sstevel@tonic-gate  {
109*0Sstevel@tonic-gate   next unless -f $_;
110*0Sstevel@tonic-gate   next if unlink($_);
111*0Sstevel@tonic-gate   chmod(0777,$_);
112*0Sstevel@tonic-gate   next if unlink($_);
113*0Sstevel@tonic-gate   carp "Cannot delete $_:$!";
114*0Sstevel@tonic-gate  }
115*0Sstevel@tonic-gate}
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate=item touch files ...
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gateMakes files exist, with current timestamp
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate=cut 
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gatesub touch {
124*0Sstevel@tonic-gate    my $t    = time;
125*0Sstevel@tonic-gate    expand_wildcards();
126*0Sstevel@tonic-gate    foreach my $file (@ARGV) {
127*0Sstevel@tonic-gate        open(FILE,">>$file") || die "Cannot write $file:$!";
128*0Sstevel@tonic-gate        close(FILE);
129*0Sstevel@tonic-gate        utime($t,$t,$file);
130*0Sstevel@tonic-gate    }
131*0Sstevel@tonic-gate}
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate=item mv source... destination
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gateMoves source to destination.
136*0Sstevel@tonic-gateMultiple sources are allowed if destination is an existing directory.
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate=cut 
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gatesub mv {
141*0Sstevel@tonic-gate    my $dst = pop(@ARGV);
142*0Sstevel@tonic-gate    expand_wildcards();
143*0Sstevel@tonic-gate    croak("Too many arguments") if (@ARGV > 1 && ! -d $dst);
144*0Sstevel@tonic-gate    foreach my $src (@ARGV) {
145*0Sstevel@tonic-gate        move($src,$dst);
146*0Sstevel@tonic-gate    }
147*0Sstevel@tonic-gate}
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate=item cp source... destination
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gateCopies source to destination.
152*0Sstevel@tonic-gateMultiple sources are allowed if destination is an existing directory.
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate=cut
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gatesub cp {
157*0Sstevel@tonic-gate    my $dst = pop(@ARGV);
158*0Sstevel@tonic-gate    expand_wildcards();
159*0Sstevel@tonic-gate    croak("Too many arguments") if (@ARGV > 1 && ! -d $dst);
160*0Sstevel@tonic-gate    foreach my $src (@ARGV) {
161*0Sstevel@tonic-gate        copy($src,$dst);
162*0Sstevel@tonic-gate    }
163*0Sstevel@tonic-gate}
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate=item chmod mode files...
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gateSets UNIX like permissions 'mode' on all the files.  e.g. 0666
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate=cut 
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gatesub chmod {
172*0Sstevel@tonic-gate    my $mode = shift(@ARGV);
173*0Sstevel@tonic-gate    expand_wildcards();
174*0Sstevel@tonic-gate    chmod(oct $mode,@ARGV) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
175*0Sstevel@tonic-gate}
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate=item mkpath directory...
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gateCreates directory, including any parent directories.
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate=cut 
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gatesub mkpath
184*0Sstevel@tonic-gate{
185*0Sstevel@tonic-gate expand_wildcards();
186*0Sstevel@tonic-gate File::Path::mkpath([@ARGV],0,0777);
187*0Sstevel@tonic-gate}
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gate=item test_f file
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gateTests if a file exists
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate=cut 
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gatesub test_f
196*0Sstevel@tonic-gate{
197*0Sstevel@tonic-gate exit !-f shift(@ARGV);
198*0Sstevel@tonic-gate}
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate1;
202*0Sstevel@tonic-gate__END__
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gate=back
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate=head1 BUGS
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gateShould probably be Auto/Self loaded.
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate=head1 SEE ALSO
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gateExtUtils::MakeMaker, ExtUtils::MM_Unix, ExtUtils::MM_Win32
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate=head1 AUTHOR
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gateNick Ing-Simmons <F<nick@ni-s.u-net.com>>.
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate=cut
219*0Sstevel@tonic-gate
220