1IPC::Cmd(3pm)          Perl Programmers Reference Guide          IPC::Cmd(3pm)
2
3
4

NAME

6       IPC::Cmd - finding and running system commands made easy
7

SYNOPSIS

9           use IPC::Cmd qw[can_run run run_forked];
10
11           my $full_path = can_run('wget') or warn 'wget is not installed!';
12
13           ### commands can be arrayrefs or strings ###
14           my $cmd = "$full_path -b theregister.co.uk";
15           my $cmd = [$full_path, '-b', 'theregister.co.uk'];
16
17           ### in scalar context ###
18           my $buffer;
19           if( scalar run( command => $cmd,
20                           verbose => 0,
21                           buffer  => \$buffer,
22                           timeout => 20 )
23           ) {
24               print "fetched webpage successfully: $buffer\n";
25           }
26
27
28           ### in list context ###
29           my( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
30                   run( command => $cmd, verbose => 0 );
31
32           if( $success ) {
33               print "this is what the command printed:\n";
34               print join "", @$full_buf;
35           }
36
37           ### check for features
38           print "IPC::Open3 available: "  . IPC::Cmd->can_use_ipc_open3;
39           print "IPC::Run available: "    . IPC::Cmd->can_use_ipc_run;
40           print "Can capture buffer: "    . IPC::Cmd->can_capture_buffer;
41
42           ### don't have IPC::Cmd be verbose, ie don't print to stdout or
43           ### stderr when running commands -- default is '0'
44           $IPC::Cmd::VERBOSE = 0;
45

DESCRIPTION

47       IPC::Cmd allows you to run commands, interactively if desired, platform
48       independent but have them still work.
49
50       The "can_run" function can tell you if a certain binary is installed
51       and if so where, whereas the "run" function can actually execute any of
52       the commands you give it and give you a clear return value, as well as
53       adhere to your verbosity settings.
54

CLASS METHODS

56   $ipc_run_version = IPC::Cmd->can_use_ipc_run( [VERBOSE] )
57       Utility function that tells you if "IPC::Run" is available.  If the
58       verbose flag is passed, it will print diagnostic messages if "IPC::Run"
59       can not be found or loaded.
60
61   $ipc_open3_version = IPC::Cmd->can_use_ipc_open3( [VERBOSE] )
62       Utility function that tells you if "IPC::Open3" is available.  If the
63       verbose flag is passed, it will print diagnostic messages if
64       "IPC::Open3" can not be found or loaded.
65
66   $bool = IPC::Cmd->can_capture_buffer
67       Utility function that tells you if "IPC::Cmd" is capable of capturing
68       buffers in it's current configuration.
69
70   $bool = IPC::Cmd->can_use_run_forked
71       Utility function that tells you if "IPC::Cmd" is capable of providing
72       "run_forked" on the current platform.
73

FUNCTIONS

75   $path = can_run( PROGRAM );
76       "can_run" takes but a single argument: the name of a binary you wish to
77       locate. "can_run" works much like the unix binary "which" or the bash
78       command "type", which scans through your path, looking for the
79       requested binary .
80
81       Unlike "which" and "type", this function is platform independent and
82       will also work on, for example, Win32.
83
84       It will return the full path to the binary you asked for if it was
85       found, or "undef" if it was not.
86
87   $ok | ($ok, $err, $full_buf, $stdout_buff, $stderr_buff) = run( command =>
88       COMMAND, [verbose => BOOL, buffer => \$SCALAR, timeout => DIGIT] );
89       "run" takes 4 arguments:
90
91       command
92           This is the command to execute. It may be either a string or an
93           array reference.  This is a required argument.
94
95           See CAVEATS for remarks on how commands are parsed and their
96           limitations.
97
98       verbose
99           This controls whether all output of a command should also be
100           printed to STDOUT/STDERR or should only be trapped in buffers
101           (NOTE: buffers require "IPC::Run" to be installed or your system
102           able to work with "IPC::Open3").
103
104           It will default to the global setting of $IPC::Cmd::VERBOSE, which
105           by default is 0.
106
107       buffer
108           This will hold all the output of a command. It needs to be a
109           reference to a scalar.  Note that this will hold both the STDOUT
110           and STDERR messages, and you have no way of telling which is which.
111           If you require this distinction, run the "run" command in list
112           context and inspect the individual buffers.
113
114           Of course, this requires that the underlying call supports buffers.
115           See the note on buffers right above.
116
117       timeout
118           Sets the maximum time the command is allowed to run before
119           aborting, using the built-in "alarm()" call. If the timeout is
120           triggered, the "errorcode" in the return value will be set to an
121           object of the "IPC::Cmd::TimeOut" class. See the "errorcode"
122           section below for details.
123
124           Defaults to 0, meaning no timeout is set.
125
126       "run" will return a simple "true" or "false" when called in scalar
127       context.  In list context, you will be returned a list of the following
128       items:
129
130       success
131           A simple boolean indicating if the command executed without errors
132           or not.
133
134       error message
135           If the first element of the return value (success) was 0, then some
136           error occurred. This second element is the error message the
137           command you requested exited with, if available. This is generally
138           a pretty printed value of $? or $@. See "perldoc perlvar" for
139           details on what they can contain.  If the error was a timeout, the
140           "error message" will be prefixed with the string
141           "IPC::Cmd::TimeOut", the timeout class.
142
143       full_buffer
144           This is an arrayreference containing all the output the command
145           generated.  Note that buffers are only available if you have
146           "IPC::Run" installed, or if your system is able to work with
147           "IPC::Open3" -- See below).  This element will be "undef" if this
148           is not the case.
149
150       out_buffer
151           This is an arrayreference containing all the output sent to STDOUT
152           the command generated.  Note that buffers are only available if you
153           have "IPC::Run" installed, or if your system is able to work with
154           "IPC::Open3" -- See below).  This element will be "undef" if this
155           is not the case.
156
157       error_buffer
158           This is an arrayreference containing all the output sent to STDERR
159           the command generated.  Note that buffers are only available if you
160           have "IPC::Run" installed, or if your system is able to work with
161           "IPC::Open3" -- See below).  This element will be "undef" if this
162           is not the case.
163
164       See the "HOW IT WORKS" Section below to see how "IPC::Cmd" decides what
165       modules or function calls to use when issuing a command.
166
167   $hashref = run_forked( command => COMMAND, { child_stdin => SCALAR, timeout
168       => DIGIT, stdout_handler => CODEREF, stderr_handler => CODEREF} );
169       "run_forked" is used to execute some program, optionally feed it with
170       some input, get its return code and output (both stdout and stderr into
171       seperate buffers).  In addition it allows to terminate the program
172       which take too long to finish.
173
174       The important and distinguishing feature of run_forked is execution
175       timeout which at first seems to be quite a simple task but if you think
176       that the program which you're spawning might spawn some children itself
177       (which in their turn could do the same and so on) it turns out to be
178       not a simple issue.
179
180       "run_forked" is designed to survive and successfully terminate almost
181       any long running task, even a fork bomb in case your system has the
182       resources to survive during given timeout.
183
184       This is achieved by creating separate watchdog process which spawns the
185       specified program in a separate process session and supervises it:
186       optionally feeds it with input, stores its exit code, stdout and
187       stderr, terminates it in case it runs longer than specified.
188
189       Invocation requires the command to be executed and optionally a hashref
190       of options:
191
192       "timeout"
193           Specify in seconds how long the command may run for before it is
194           killed with with SIG_KILL (9) which effectively terminates it and
195           all of its children (direct or indirect).
196
197       "child_stdin"
198           Specify some text that will be passed into "STDIN" of the executed
199           program.
200
201       "stdout_handler"
202           You may provide a coderef of a subroutine that will be called a
203           portion of data is received on stdout from the executing program.
204
205       "stderr_handler"
206           You may provide a coderef of a subroutine that will be called a
207           portion of data is received on stderr from the executing program.
208
209       "run_forked" will return a HASHREF with the following keys:
210
211       "exit_code"
212           The exit code of the executed program.
213
214       "timeout"
215           The number of seconds the program ran for before being terminated,
216           or 0 if no timeout occurred.
217
218       "stdout"
219           Holds the standard output of the executed command (or empty string
220           if there were no stdout output; it's always defined!)
221
222       "stderr"
223           Holds the standard error of the executed command (or empty string
224           if there were no stderr output; it's always defined!)
225
226       "merged"
227           Holds the standard output and error of the executed command merged
228           into one stream (or empty string if there were no output at all;
229           it's always defined!)
230
231       "err_msg"
232           Holds some explanation in the case of an error.
233
234   $q = QUOTE
235       Returns the character used for quoting strings on this platform. This
236       is usually a "'" (single quote) on most systems, but some systems use
237       different quotes. For example, "Win32" uses """ (double quote).
238
239       You can use it as follows:
240
241         use IPC::Cmd qw[run QUOTE];
242         my $cmd = q[echo ] . QUOTE . q[foo bar] . QUOTE;
243
244       This makes sure that "foo bar" is treated as a string, rather than two
245       seperate arguments to the "echo" function.
246
247       __END__
248

HOW IT WORKS

250       "run" will try to execute your command using the following logic:
251
252       ·   If you have "IPC::Run" installed, and the variable
253           $IPC::Cmd::USE_IPC_RUN is set to true (See the "GLOBAL VARIABLES"
254           Section) use that to execute the command. You will have the full
255           output available in buffers, interactive commands are sure to work
256           and you are guaranteed to have your verbosity settings honored
257           cleanly.
258
259       ·   Otherwise, if the variable $IPC::Cmd::USE_IPC_OPEN3 is set to true
260           (See the "GLOBAL VARIABLES" Section), try to execute the command
261           using "IPC::Open3". Buffers will be available on all platforms
262           except "Win32", interactive commands will still execute cleanly,
263           and also your verbosity settings will be adhered to nicely;
264
265       ·   Otherwise, if you have the verbose argument set to true, we fall
266           back to a simple system() call. We cannot capture any buffers, but
267           interactive commands will still work.
268
269       ·   Otherwise we will try and temporarily redirect STDERR and STDOUT,
270           do a system() call with your command and then re-open STDERR and
271           STDOUT.  This is the method of last resort and will still allow you
272           to execute your commands cleanly. However, no buffers will be
273           available.
274

Global Variables

276       The behaviour of IPC::Cmd can be altered by changing the following
277       global variables:
278
279   $IPC::Cmd::VERBOSE
280       This controls whether IPC::Cmd will print any output from the commands
281       to the screen or not. The default is 0;
282
283   $IPC::Cmd::USE_IPC_RUN
284       This variable controls whether IPC::Cmd will try to use IPC::Run when
285       available and suitable. Defaults to true if you are on "Win32".
286
287   $IPC::Cmd::USE_IPC_OPEN3
288       This variable controls whether IPC::Cmd will try to use IPC::Open3 when
289       available and suitable. Defaults to true.
290
291   $IPC::Cmd::WARN
292       This variable controls whether run time warnings should be issued, like
293       the failure to load an "IPC::*" module you explicitly requested.
294
295       Defaults to true. Turn this off at your own risk.
296

Caveats

298       Whitespace and IPC::Open3 / system()
299           When using "IPC::Open3" or "system", if you provide a string as the
300           "command" argument, it is assumed to be appropriately escaped. You
301           can use the "QUOTE" constant to use as a portable quote character
302           (see above).  However, if you provide and "Array Reference",
303           special rules apply:
304
305           If your command contains "Special Characters" (< > | &), it will be
306           internally stringified before executing the command, to avoid that
307           these special characters are escaped and passed as arguments
308           instead of retaining their special meaning.
309
310           However, if the command contained arguments that contained
311           whitespace, stringifying the command would loose the significance
312           of the whitespace.  Therefor, "IPC::Cmd" will quote any arguments
313           containing whitespace in your command if the command is passed as
314           an arrayref and contains special characters.
315
316       Whitespace and IPC::Run
317           When using "IPC::Run", if you provide a string as the "command"
318           argument, the string will be split on whitespace to determine the
319           individual elements of your command. Although this will usually
320           just Do What You Mean, it may break if you have files or commands
321           with whitespace in them.
322
323           If you do not wish this to happen, you should provide an array
324           reference, where all parts of your command are already separated
325           out.  Note however, if there's extra or spurious whitespace in
326           these parts, the parser or underlying code may not interpret it
327           correctly, and cause an error.
328
329           Example: The following code
330
331               gzip -cdf foo.tar.gz | tar -xf -
332
333           should either be passed as
334
335               "gzip -cdf foo.tar.gz | tar -xf -"
336
337           or as
338
339               ['gzip', '-cdf', 'foo.tar.gz', '|', 'tar', '-xf', '-']
340
341           But take care not to pass it as, for example
342
343               ['gzip -cdf foo.tar.gz', '|', 'tar -xf -']
344
345           Since this will lead to issues as described above.
346
347       IO Redirect
348           Currently it is too complicated to parse your command for IO
349           Redirections. For capturing STDOUT or STDERR there is a work around
350           however, since you can just inspect your buffers for the contents.
351
352       Interleaving STDOUT/STDERR
353           Neither IPC::Run nor IPC::Open3 can interleave STDOUT and STDERR.
354           For short bursts of output from a program, ie this sample:
355
356               for ( 1..4 ) {
357                   $_ % 2 ? print STDOUT $_ : print STDERR $_;
358               }
359
360           IPC::[Run|Open3] will first read all of STDOUT, then all of STDERR,
361           meaning the output looks like 1 line on each, namely '13' on STDOUT
362           and '24' on STDERR.
363
364           It should have been 1, 2, 3, 4.
365
366           This has been recorded in rt.cpan.org as bug #37532: Unable to
367           interleave STDOUT and STDERR
368

See Also

370       "IPC::Run", "IPC::Open3"
371

ACKNOWLEDGEMENTS

373       Thanks to James Mastros and Martijn van der Streek for their help in
374       getting IPC::Open3 to behave nicely.
375
376       Thanks to Petya Kohts for the "run_forked" code.
377

BUG REPORTS

379       Please report bugs or other issues to <bug-ipc-cmd@rt.cpan.org>.
380

AUTHOR

382       This module by Jos Boumans <kane@cpan.org>.
383
385       This library is free software; you may redistribute and/or modify it
386       under the same terms as Perl itself.
387
388
389
390perl v5.12.4                      2011-06-07                     IPC::Cmd(3pm)
Impressum