5 use File::Spec::Functions;
7 our($capture_stderr, $raw, $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"};
29 # NOTE: this is used to enable constant folding in
30 # expressions like (OS eq 'MSWin32') and
31 # (OS eq 'os2') just like it happened in 0.6 version
32 # which used eval "string" to install subs on the fly.
33 use constant OS => $^O;
39 $sub = _make_cmd($cmd);
40 $sub = $shell->_make_cmd($cmd);
42 Creates a closure which invokes the system command C<$cmd>.
47 shift if ref $_[0] && $_[0]->isa( 'Shell' );
49 my $null = File::Spec::Functions::devnull();
50 $Shell::capture_stderr ||= 0;
51 # closing over $^O, $cmd, and $null
53 shift if ref $_[0] && $_[0]->isa( 'Shell' );
55 $Shell::capture_stderr == 1 ? `$cmd 2>&1` :
56 $Shell::capture_stderr == -1 ? `$cmd 2>$null` :
58 } elsif (OS eq 'os2') {
59 local(*SAVEOUT, *READ, *WRITE);
61 open SAVEOUT, '>&STDOUT' or die;
62 pipe READ, WRITE or die;
63 open STDOUT, '>&WRITE' or die;
66 my $pid = system(1, $cmd, @_);
67 die "Can't execute $cmd: $!\n" if $pid < 0;
69 open STDOUT, '>&SAVEOUT' or die;
87 unless( $Shell::raw ){
88 if (OS eq 'MSWin32') {
89 # XXX this special-casing should not be needed
90 # if we do quoting right on Windows. :-(
92 # First, escape all quotes. Cover the case where we
93 # want to pass along a quote preceded by a backslash
94 # (i.e., C<"param \""" end">).
95 # Ugly, yup? You know, windoze.
96 # Enclose in quotes only the parameters that need it:
97 # try this: c:> dir "/w"
98 # and this: c:> dir /w
102 $_ = qq["$_"] if /\s/;
111 push @arr, '2>&1' if $Shell::capture_stderr == 1;
112 push @arr, '2>$null' if $Shell::capture_stderr == -1;
113 open(SUBPROC, join(' ', $cmd, @arr, '|'))
114 or die "Can't exec $cmd: $!\n";
117 close SUBPROC; # XXX Oughta use a destructor.
130 shift if ref $_[0] && $_[0]->isa( 'Shell' );
134 *$AUTOLOAD = _make_cmd($cmd);
144 Shell - run shell commands transparently within perl
148 use Shell qw(cat ps cp);
149 $passwd = cat('</etc/passwd');
150 @pslines = ps('-ww'),
151 cp("/etc/passwd", "/tmp/passwd");
161 This package is included as a show case, illustrating a few Perl features.
162 It shouldn't be used for production programs. Although it does provide a
163 simple interface for obtaining the standard output of arbitrary commands,
164 there may be better ways of achieving what you need.
166 Running shell commands while obtaining standard output can be done with the
167 C<qx/STRING/> operator, or by calling C<open> with a filename expression that
168 ends with C<|>, giving you the option to process one line at a time.
169 If you don't need to process standard output at all, you might use C<system>
170 (in preference of doing a print with the collected standard output).
172 Since Shell.pm and all of the aforementioned techniques use your system's
173 shell to call some local command, none of them is portable across different
174 systems. Note, however, that there are several built in functions and
175 library packages providing portable implementations of functions operating
176 on files, such as: C<glob>, C<link> and C<unlink>, C<mkdir> and C<rmdir>,
177 C<rename>, C<File::Compare>, C<File::Copy>, C<File::Find> etc.
179 Using Shell.pm while importing C<foo> creates a subroutine C<foo> in the
180 namespace of the importing package. Calling C<foo> with arguments C<arg1>,
181 C<arg2>,... results in a shell command C<foo arg1 arg2...>, where the
182 function name and the arguments are joined with a blank. (See the subsection
183 on Escaping magic characters.) Since the result is essentially a command
184 line to be passed to the shell, your notion of arguments to the Perl
185 function is not necessarily identical to what the shell treats as a
186 command line token, to be passed as an individual argument to the program.
187 Furthermore, note that this implies that C<foo> is callable by file name
188 only, which frequently depends on the setting of the program's environment.
190 Creating a Shell object gives you the opportunity to call any command
191 in the usual OO notation without requiring you to announce it in the
192 C<use Shell> statement. Don't assume any additional semantics being
193 associated with a Shell object: in no way is it similar to a shell
194 process with its environment or current working directory or any
197 =head2 Escaping Magic Characters
199 It is, in general, impossible to take care of quoting the shell's
200 magic characters. For some obscure reason, however, Shell.pm quotes
201 apostrophes (C<'>) and backslashes (C<\>) on UNIX, and spaces and
202 quotes (C<">) on Windows.
206 If you set $Shell::capture_stderr to true, the module will attempt to
207 capture the standard error output of the process as well. This is
208 done by adding C<2E<gt>&1> to the command line, so don't try this on
209 a system not supporting this redirection.
211 If you set $Shell::raw to true no quoting whatsoever is done.
215 Quoting should be off by default.
217 It isn't possible to call shell built in commands, but it can be
218 done by using a workaround, e.g. shell( '-c', 'set' ).
220 Capturing standard error does not work on some systems (e.g. VMS).
224 Date: Thu, 22 Sep 94 16:18:16 -0700
225 Message-Id: <9409222318.AA17072@scalpel.netlabs.com>
226 To: perl5-porters@isu.edu
227 From: Larry Wall <lwall@scalpel.netlabs.com>
228 Subject: a new module I just wrote
230 Here's one that'll whack your mind a little out.
236 $foo = echo("howdy", "<funny>", "world");
239 $passwd = cat("</etc/passwd");
245 cp("/etc/passwd", "/etc/passwd.orig");
247 That's maybe too gonzo. It actually exports an AUTOLOAD to the current
248 package (and uncovered a bug in Beta 3, by the way). Maybe the usual
251 use Shell qw(echo cat ps cp);
255 Changes by Jenda@Krynicky.cz and Dave Cottle <d.cottle@csc.canterbury.ac.nz>.
257 Changes for OO syntax and bug fixes by Casey West <casey@geeknest.com>.
259 C<$Shell::raw> and pod rewrite by Wolfgang Laun.
261 Rewritten to use closures rather than C<eval "string"> by Adriano Ferreira.