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