1Shell(3pm) Perl Programmers Reference Guide Shell(3pm)
2
3
4
6 Shell - run shell commands transparently within perl
7
9 use Shell qw(cat ps cp);
10 $passwd = cat('</etc/passwd');
11 @pslines = ps('-ww'),
12 cp("/etc/passwd", "/etc/passwd.orig");
13
14 # object oriented
15 my $sh = Shell->new;
16 print $sh->ls('-l');
17
19 Caveats
20
21 This package is included as a show case, illustrating a few Perl fea‐
22 tures. It shouldn't be used for production programs. Although it does
23 provide a simple interface for obtaining the standard output of arbi‐
24 trary commands, there may be better ways of achieving what you need.
25
26 Running shell commands while obtaining standard output can be done with
27 the "qx/STRING/" operator, or by calling "open" with a filename expres‐
28 sion that ends with "⎪", giving you the option to process one line at a
29 time. If you don't need to process standard output at all, you might
30 use "system" (in preference of doing a print with the collected stan‐
31 dard output).
32
33 Since Shell.pm and all of the aforementioned techniques use your sys‐
34 tem's shell to call some local command, none of them is portable across
35 different systems. Note, however, that there are several built in func‐
36 tions and library packages providing portable implementations of func‐
37 tions operating on files, such as: "glob", "link" and "unlink", "mkdir"
38 and "rmdir", "rename", "File::Compare", "File::Copy", "File::Find" etc.
39
40 Using Shell.pm while importing "foo" creates a subroutine "foo" in the
41 namespace of the importing package. Calling "foo" with arguments
42 "arg1", "arg2",... results in a shell command "foo arg1 arg2...", where
43 the function name and the arguments are joined with a blank. (See the
44 subsection on Escaping magic characters.) Since the result is essen‐
45 tially a command line to be passed to the shell, your notion of argu‐
46 ments to the Perl function is not necessarily identical to what the
47 shell treats as a command line token, to be passed as an individual
48 argument to the program. Furthermore, note that this implies that
49 "foo" is callable by file name only, which frequently depends on the
50 setting of the program's environment.
51
52 Creating a Shell object gives you the opportunity to call any command
53 in the usual OO notation without requiring you to announce it in the
54 "use Shell" statement. Don't assume any additional semantics being
55 associated with a Shell object: in no way is it similar to a shell
56 process with its environment or current working directory or any other
57 setting.
58
59 Escaping Magic Characters
60
61 It is, in general, impossible to take care of quoting the shell's magic
62 characters. For some obscure reason, however, Shell.pm quotes apostro‐
63 phes ("'") and backslashes ("\") on UNIX, and spaces and quotes (""")
64 on Windows.
65
66 Configuration
67
68 If you set $Shell::capture_stderr to true, the module will attempt to
69 capture the standard error output of the process as well. This is done
70 by adding "2>&1" to the command line, so don't try this on a system not
71 supporting this redirection.
72
73 If you set $Shell::raw to true no quoting whatsoever is done.
74
76 Quoting should be off by default.
77
78 It isn't possible to call shell built in commands, but it can be done
79 by using a workaround, e.g. shell( '-c', 'set' ).
80
81 Capturing standard error does not work on some systems (e.g. VMS).
82
84 Date: Thu, 22 Sep 94 16:18:16 -0700
85 Message-Id: <9409222318.AA17072@scalpel.netlabs.com>
86 To: perl5-porters@isu.edu
87 From: Larry Wall <lwall@scalpel.netlabs.com>
88 Subject: a new module I just wrote
89
90 Here's one that'll whack your mind a little out.
91
92 #!/usr/bin/perl
93
94 use Shell;
95
96 $foo = echo("howdy", "<funny>", "world");
97 print $foo;
98
99 $passwd = cat("</etc/passwd");
100 print $passwd;
101
102 sub ps;
103 print ps -ww;
104
105 cp("/etc/passwd", "/etc/passwd.orig");
106
107 That's maybe too gonzo. It actually exports an AUTOLOAD to the current
108 package (and uncovered a bug in Beta 3, by the way). Maybe the usual
109 usage should be
110
111 use Shell qw(echo cat ps cp);
112
113 Larry Wall
114
115 Changes by Jenda@Krynicky.cz and Dave Cottle <d.cottle@csc.canter‐
116 bury.ac.nz>.
117
118 Changes for OO syntax and bug fixes by Casey West <casey@geeknest.com>.
119
120 $Shell::raw and pod rewrite by Wolfgang Laun.
121
122
123
124perl v5.8.8 2001-09-21 Shell(3pm)