5 use File::Spec::Functions;
7 our($capture_stderr, $VERSION, $AUTOLOAD);
11 sub new { bless \my $foo, shift }
16 my ($callpack, $callfile, $callline) = caller;
23 foreach my $sym (@EXPORT) {
25 *{"${callpack}::$sym"} = \&{"Shell::$sym"};
30 shift if ref $_[0] && $_[0]->isa( 'Shell' );
33 my $null = File::Spec::Functions::devnull();
34 $Shell::capture_stderr ||= 0;
37 shift if ref \$_[0] && \$_[0]->isa( 'Shell' );
39 \$Shell::capture_stderr == 1 ? `$cmd 2>&1` :
40 \$Shell::capture_stderr == -1 ? `$cmd 2>$null` :
42 } elsif ('$^O' eq 'os2') {
43 local(\*SAVEOUT, \*READ, \*WRITE);
45 open SAVEOUT, '>&STDOUT' or die;
46 pipe READ, WRITE or die;
47 open STDOUT, '>&WRITE' or die;
50 my \$pid = system(1, '$cmd', \@_);
51 die "Can't execute $cmd: \$!\\n" if \$pid < 0;
53 open STDOUT, '>&SAVEOUT' or die;
71 if ('$^O' eq 'MSWin32') {
72 # XXX this special-casing should not be needed
73 # if we do quoting right on Windows. :-(
75 # First, escape all quotes. Cover the case where we
76 # want to pass along a quote preceded by a backslash
77 # (i.e., C<"param \\""" end">).
78 # Ugly, yup? You know, windoze.
79 # Enclose in quotes only the parameters that need it:
80 # try this: c:\> dir "/w"
81 # and this: c:\> dir /w
84 s/\\\\\\\\"/\\\\\\\\"""/g;
85 \$_ = qq["\$_"] if /\\s/;
89 s/(['\\\\])/\\\\\$1/g;
93 push \@arr, '2>&1' if \$Shell::capture_stderr == 1;
94 push \@arr, '2>$null' if \$Shell::capture_stderr == -1;
95 open(SUBPROC, join(' ', '$cmd', \@arr, '|'))
96 or die "Can't exec $cmd: \$!\\n";
99 close SUBPROC; # XXX Oughta use a destructor.
103 my \$ret = <SUBPROC>;
121 Shell - run shell commands transparently within perl
129 Date: Thu, 22 Sep 94 16:18:16 -0700
130 Message-Id: <9409222318.AA17072@scalpel.netlabs.com>
131 To: perl5-porters@isu.edu
132 From: Larry Wall <lwall@scalpel.netlabs.com>
133 Subject: a new module I just wrote
135 Here's one that'll whack your mind a little out.
141 $foo = echo("howdy", "<funny>", "world");
144 $passwd = cat("</etc/passwd");
150 cp("/etc/passwd", "/etc/passwd.orig");
152 That's maybe too gonzo. It actually exports an AUTOLOAD to the current
153 package (and uncovered a bug in Beta 3, by the way). Maybe the usual
156 use Shell qw(echo cat ps cp);
161 If you set $Shell::capture_stderr to 1, the module will attempt to
162 capture the STDERR of the process as well.
164 If you set $Shell::capture_stderr to -1, the module will discard the
165 STDERR of the process.
167 The module now should work on Win32.
171 There seemed to be a problem where all arguments to a shell command were
172 quoted before being executed. As in the following example:
187 and of course, this is wrong.
189 I have fixed this bug, it was brought up by Wolfgang Laun [ID 20000326.008]
193 =head2 OBJECT ORIENTED SYNTAX
195 Shell now has an OO interface. Good for namespace conservation
196 and shell representation.
208 Changes by Jenda@Krynicky.cz and Dave Cottle <d.cottle@csc.canterbury.ac.nz>
210 Changes and bug fixes by Casey West <casey@geeknest.com>