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