| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "IPC::Cmd 3perl" |
| 58 |
.TH IPC::Cmd 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
IPC::Cmd \- finding and running system commands made easy |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 1 |
| 68 |
\& use IPC::Cmd qw[can_run run run_forked]; |
| 69 |
\& |
| 70 |
\& my $full_path = can_run(\*(Aqwget\*(Aq) or warn \*(Aqwget is not installed!\*(Aq; |
| 71 |
\& |
| 72 |
\& ### commands can be arrayrefs or strings ### |
| 73 |
\& my $cmd = "$full_path \-b theregister.co.uk"; |
| 74 |
\& my $cmd = [$full_path, \*(Aq\-b\*(Aq, \*(Aqtheregister.co.uk\*(Aq]; |
| 75 |
\& |
| 76 |
\& ### in scalar context ### |
| 77 |
\& my $buffer; |
| 78 |
\& if( scalar run( command => $cmd, |
| 79 |
\& verbose => 0, |
| 80 |
\& buffer => \e$buffer, |
| 81 |
\& timeout => 20 ) |
| 82 |
\& ) { |
| 83 |
\& print "fetched webpage successfully: $buffer\en"; |
| 84 |
\& } |
| 85 |
\& |
| 86 |
\& |
| 87 |
\& ### in list context ### |
| 88 |
\& my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) = |
| 89 |
\& run( command => $cmd, verbose => 0 ); |
| 90 |
\& |
| 91 |
\& if( $success ) { |
| 92 |
\& print "this is what the command printed:\en"; |
| 93 |
\& print join "", @$full_buf; |
| 94 |
\& } |
| 95 |
\& |
| 96 |
\& ### run_forked example ### |
| 97 |
\& my $result = run_forked("$full_path \-q \-O \- theregister.co.uk", {\*(Aqtimeout\*(Aq => 20}); |
| 98 |
\& if ($result\->{\*(Aqexit_code\*(Aq} eq 0 && !$result\->{\*(Aqtimeout\*(Aq}) { |
| 99 |
\& print "this is what wget returned:\en"; |
| 100 |
\& print $result\->{\*(Aqstdout\*(Aq}; |
| 101 |
\& } |
| 102 |
\& |
| 103 |
\& ### check for features |
| 104 |
\& print "IPC::Open3 available: " . IPC::Cmd\->can_use_ipc_open3; |
| 105 |
\& print "IPC::Run available: " . IPC::Cmd\->can_use_ipc_run; |
| 106 |
\& print "Can capture buffer: " . IPC::Cmd\->can_capture_buffer; |
| 107 |
\& |
| 108 |
\& ### don\*(Aqt have IPC::Cmd be verbose, ie don\*(Aqt print to stdout or |
| 109 |
\& ### stderr when running commands \-\- default is \*(Aq0\*(Aq |
| 110 |
\& $IPC::Cmd::VERBOSE = 0; |
| 111 |
.Ve |
| 112 |
.SH DESCRIPTION |
| 113 |
.IX Header "DESCRIPTION" |
| 114 |
IPC::Cmd allows you to run commands platform independently, |
| 115 |
interactively if desired, but have them still work. |
| 116 |
.PP |
| 117 |
The \f(CW\*(C`can_run\*(C'\fR function can tell you if a certain binary is installed |
| 118 |
and if so where, whereas the \f(CW\*(C`run\*(C'\fR function can actually execute any |
| 119 |
of the commands you give it and give you a clear return value, as well |
| 120 |
as adhere to your verbosity settings. |
| 121 |
.SH "CLASS METHODS" |
| 122 |
.IX Header "CLASS METHODS" |
| 123 |
.ie n .SS "$ipc_run_version = IPC::Cmd\->can_use_ipc_run( [VERBOSE] )" |
| 124 |
.el .SS "\f(CW$ipc_run_version\fP = IPC::Cmd\->can_use_ipc_run( [VERBOSE] )" |
| 125 |
.IX Subsection "$ipc_run_version = IPC::Cmd->can_use_ipc_run( [VERBOSE] )" |
| 126 |
Utility function that tells you if \f(CW\*(C`IPC::Run\*(C'\fR is available. |
| 127 |
If the \f(CW\*(C`verbose\*(C'\fR flag is passed, it will print diagnostic messages |
| 128 |
if IPC::Run can not be found or loaded. |
| 129 |
.ie n .SS "$ipc_open3_version = IPC::Cmd\->can_use_ipc_open3( [VERBOSE] )" |
| 130 |
.el .SS "\f(CW$ipc_open3_version\fP = IPC::Cmd\->can_use_ipc_open3( [VERBOSE] )" |
| 131 |
.IX Subsection "$ipc_open3_version = IPC::Cmd->can_use_ipc_open3( [VERBOSE] )" |
| 132 |
Utility function that tells you if \f(CW\*(C`IPC::Open3\*(C'\fR is available. |
| 133 |
If the verbose flag is passed, it will print diagnostic messages |
| 134 |
if \f(CW\*(C`IPC::Open3\*(C'\fR can not be found or loaded. |
| 135 |
.ie n .SS "$bool = IPC::Cmd\->can_capture_buffer" |
| 136 |
.el .SS "\f(CW$bool\fP = IPC::Cmd\->can_capture_buffer" |
| 137 |
.IX Subsection "$bool = IPC::Cmd->can_capture_buffer" |
| 138 |
Utility function that tells you if \f(CW\*(C`IPC::Cmd\*(C'\fR is capable of |
| 139 |
capturing buffers in it's current configuration. |
| 140 |
.ie n .SS "$bool = IPC::Cmd\->can_use_run_forked" |
| 141 |
.el .SS "\f(CW$bool\fP = IPC::Cmd\->can_use_run_forked" |
| 142 |
.IX Subsection "$bool = IPC::Cmd->can_use_run_forked" |
| 143 |
Utility function that tells you if \f(CW\*(C`IPC::Cmd\*(C'\fR is capable of |
| 144 |
providing \f(CW\*(C`run_forked\*(C'\fR on the current platform. |
| 145 |
.SH FUNCTIONS |
| 146 |
.IX Header "FUNCTIONS" |
| 147 |
.ie n .SS "$path = can_run( PROGRAM );" |
| 148 |
.el .SS "\f(CW$path\fP = can_run( PROGRAM );" |
| 149 |
.IX Subsection "$path = can_run( PROGRAM );" |
| 150 |
\&\f(CW\*(C`can_run\*(C'\fR takes only one argument: the name of a binary you wish |
| 151 |
to locate. \f(CW\*(C`can_run\*(C'\fR works much like the unix binary \f(CW\*(C`which\*(C'\fR or the bash |
| 152 |
command \f(CW\*(C`type\*(C'\fR, which scans through your path, looking for the requested |
| 153 |
binary. |
| 154 |
.PP |
| 155 |
Unlike \f(CW\*(C`which\*(C'\fR and \f(CW\*(C`type\*(C'\fR, this function is platform independent and |
| 156 |
will also work on, for example, Win32. |
| 157 |
.PP |
| 158 |
If called in a scalar context it will return the full path to the binary |
| 159 |
you asked for if it was found, or \f(CW\*(C`undef\*(C'\fR if it was not. |
| 160 |
.PP |
| 161 |
If called in a list context and the global variable \f(CW$INSTANCES\fR is a true |
| 162 |
value, it will return a list of the full paths to instances |
| 163 |
of the binary where found in \f(CW\*(C`PATH\*(C'\fR, or an empty list if it was not found. |
| 164 |
.ie n .SS "$ok | ($ok, $err, $full_buf, $stdout_buff, $stderr_buff) = run( command => COMMAND, [verbose => BOOL, buffer => \e$SCALAR, timeout => DIGIT] );" |
| 165 |
.el .SS "\f(CW$ok\fP | ($ok, \f(CW$err\fP, \f(CW$full_buf\fP, \f(CW$stdout_buff\fP, \f(CW$stderr_buff\fP) = run( command => COMMAND, [verbose => BOOL, buffer => \e$SCALAR, timeout => DIGIT] );" |
| 166 |
.IX Subsection "$ok | ($ok, $err, $full_buf, $stdout_buff, $stderr_buff) = run( command => COMMAND, [verbose => BOOL, buffer => $SCALAR, timeout => DIGIT] );" |
| 167 |
\&\f(CW\*(C`run\*(C'\fR takes 4 arguments: |
| 168 |
.IP command 4 |
| 169 |
.IX Item "command" |
| 170 |
This is the command to execute. It may be either a string or an array |
| 171 |
reference. |
| 172 |
This is a required argument. |
| 173 |
.Sp |
| 174 |
See "Caveats" for remarks on how commands are parsed and their |
| 175 |
limitations. |
| 176 |
.IP verbose 4 |
| 177 |
.IX Item "verbose" |
| 178 |
This controls whether all output of a command should also be printed |
| 179 |
to STDOUT/STDERR or should only be trapped in buffers (NOTE: buffers |
| 180 |
require IPC::Run to be installed, or your system able to work with |
| 181 |
IPC::Open3). |
| 182 |
.Sp |
| 183 |
It will default to the global setting of \f(CW$IPC::Cmd::VERBOSE\fR, |
| 184 |
which by default is 0. |
| 185 |
.IP buffer 4 |
| 186 |
.IX Item "buffer" |
| 187 |
This will hold all the output of a command. It needs to be a reference |
| 188 |
to a scalar. |
| 189 |
Note that this will hold both the STDOUT and STDERR messages, and you |
| 190 |
have no way of telling which is which. |
| 191 |
If you require this distinction, run the \f(CW\*(C`run\*(C'\fR command in list context |
| 192 |
and inspect the individual buffers. |
| 193 |
.Sp |
| 194 |
Of course, this requires that the underlying call supports buffers. See |
| 195 |
the note on buffers above. |
| 196 |
.IP timeout 4 |
| 197 |
.IX Item "timeout" |
| 198 |
Sets the maximum time the command is allowed to run before aborting, |
| 199 |
using the built-in \f(CWalarm()\fR call. If the timeout is triggered, the |
| 200 |
\&\f(CW\*(C`errorcode\*(C'\fR in the return value will be set to an object of the |
| 201 |
\&\f(CW\*(C`IPC::Cmd::TimeOut\*(C'\fR class. See the "error message" section below for |
| 202 |
details. |
| 203 |
.Sp |
| 204 |
Defaults to \f(CW0\fR, meaning no timeout is set. |
| 205 |
.PP |
| 206 |
\&\f(CW\*(C`run\*(C'\fR will return a simple \f(CW\*(C`true\*(C'\fR or \f(CW\*(C`false\*(C'\fR when called in scalar |
| 207 |
context. |
| 208 |
In list context, you will be returned a list of the following items: |
| 209 |
.IP success 4 |
| 210 |
.IX Item "success" |
| 211 |
A simple boolean indicating if the command executed without errors or |
| 212 |
not. |
| 213 |
.IP "error message" 4 |
| 214 |
.IX Item "error message" |
| 215 |
If the first element of the return value (\f(CW\*(C`success\*(C'\fR) was 0, then some |
| 216 |
error occurred. This second element is the error message the command |
| 217 |
you requested exited with, if available. This is generally a pretty |
| 218 |
printed value of \f(CW$?\fR or \f(CW$@\fR. See \f(CW\*(C`perldoc perlvar\*(C'\fR for details on |
| 219 |
what they can contain. |
| 220 |
If the error was a timeout, the \f(CW\*(C`error message\*(C'\fR will be prefixed with |
| 221 |
the string \f(CW\*(C`IPC::Cmd::TimeOut\*(C'\fR, the timeout class. |
| 222 |
.IP full_buffer 4 |
| 223 |
.IX Item "full_buffer" |
| 224 |
This is an array reference containing all the output the command |
| 225 |
generated. |
| 226 |
Note that buffers are only available if you have IPC::Run installed, |
| 227 |
or if your system is able to work with IPC::Open3 \-\- see below). |
| 228 |
Otherwise, this element will be \f(CW\*(C`undef\*(C'\fR. |
| 229 |
.IP out_buffer 4 |
| 230 |
.IX Item "out_buffer" |
| 231 |
This is an array reference containing all the output sent to STDOUT the |
| 232 |
command generated. The notes from "full_buffer" apply. |
| 233 |
.IP error_buffer 4 |
| 234 |
.IX Item "error_buffer" |
| 235 |
This is an arrayreference containing all the output sent to STDERR the |
| 236 |
command generated. The notes from "full_buffer" apply. |
| 237 |
.PP |
| 238 |
See the "HOW IT WORKS" section below to see how \f(CW\*(C`IPC::Cmd\*(C'\fR decides |
| 239 |
what modules or function calls to use when issuing a command. |
| 240 |
.ie n .SS "$hashref = run_forked( COMMAND, { child_stdin => SCALAR, timeout => DIGIT, stdout_handler => CODEREF, stderr_handler => CODEREF} );" |
| 241 |
.el .SS "\f(CW$hashref\fP = run_forked( COMMAND, { child_stdin => SCALAR, timeout => DIGIT, stdout_handler => CODEREF, stderr_handler => CODEREF} );" |
| 242 |
.IX Subsection "$hashref = run_forked( COMMAND, { child_stdin => SCALAR, timeout => DIGIT, stdout_handler => CODEREF, stderr_handler => CODEREF} );" |
| 243 |
\&\f(CW\*(C`run_forked\*(C'\fR is used to execute some program or a coderef, |
| 244 |
optionally feed it with some input, get its return code |
| 245 |
and output (both stdout and stderr into separate buffers). |
| 246 |
In addition, it allows to terminate the program |
| 247 |
if it takes too long to finish. |
| 248 |
.PP |
| 249 |
The important and distinguishing feature of run_forked |
| 250 |
is execution timeout which at first seems to be |
| 251 |
quite a simple task but if you think |
| 252 |
that the program which you're spawning |
| 253 |
might spawn some children itself (which |
| 254 |
in their turn could do the same and so on) |
| 255 |
it turns out to be not a simple issue. |
| 256 |
.PP |
| 257 |
\&\f(CW\*(C`run_forked\*(C'\fR is designed to survive and |
| 258 |
successfully terminate almost any long running task, |
| 259 |
even a fork bomb in case your system has the resources |
| 260 |
to survive during given timeout. |
| 261 |
.PP |
| 262 |
This is achieved by creating separate watchdog process |
| 263 |
which spawns the specified program in a separate |
| 264 |
process session and supervises it: optionally |
| 265 |
feeds it with input, stores its exit code, |
| 266 |
stdout and stderr, terminates it in case |
| 267 |
it runs longer than specified. |
| 268 |
.PP |
| 269 |
Invocation requires the command to be executed or a coderef and optionally a hashref of options: |
| 270 |
.ie n .IP """timeout""" 4 |
| 271 |
.el .IP \f(CWtimeout\fR 4 |
| 272 |
.IX Item "timeout" |
| 273 |
Specify in seconds how long to run the command before it is killed with SIG_KILL (9), |
| 274 |
which effectively terminates it and all of its children (direct or indirect). |
| 275 |
.ie n .IP """child_stdin""" 4 |
| 276 |
.el .IP \f(CWchild_stdin\fR 4 |
| 277 |
.IX Item "child_stdin" |
| 278 |
Specify some text that will be passed into the \f(CW\*(C`STDIN\*(C'\fR of the executed program. |
| 279 |
.ie n .IP """stdout_handler""" 4 |
| 280 |
.el .IP \f(CWstdout_handler\fR 4 |
| 281 |
.IX Item "stdout_handler" |
| 282 |
Coderef of a subroutine to call when a portion of data is received on |
| 283 |
STDOUT from the executing program. |
| 284 |
.ie n .IP """stderr_handler""" 4 |
| 285 |
.el .IP \f(CWstderr_handler\fR 4 |
| 286 |
.IX Item "stderr_handler" |
| 287 |
Coderef of a subroutine to call when a portion of data is received on |
| 288 |
STDERR from the executing program. |
| 289 |
.ie n .IP """wait_loop_callback""" 4 |
| 290 |
.el .IP \f(CWwait_loop_callback\fR 4 |
| 291 |
.IX Item "wait_loop_callback" |
| 292 |
Coderef of a subroutine to call inside of the main waiting loop |
| 293 |
(while \f(CW\*(C`run_forked\*(C'\fR waits for the external to finish or fail). |
| 294 |
It is useful to stop running external process before it ends |
| 295 |
by itself, e.g. |
| 296 |
.Sp |
| 297 |
.Vb 8 |
| 298 |
\& my $r = run_forked("some external command", { |
| 299 |
\& \*(Aqwait_loop_callback\*(Aq => sub { |
| 300 |
\& if (condition) { |
| 301 |
\& kill(1, $$); |
| 302 |
\& } |
| 303 |
\& }, |
| 304 |
\& \*(Aqterminate_on_signal\*(Aq => \*(AqHUP\*(Aq, |
| 305 |
\& }); |
| 306 |
.Ve |
| 307 |
.Sp |
| 308 |
Combined with \f(CW\*(C`stdout_handler\*(C'\fR and \f(CW\*(C`stderr_handler\*(C'\fR allows terminating |
| 309 |
external command based on its output. Could also be used as a timer |
| 310 |
without engaging with alarm (signals). |
| 311 |
.Sp |
| 312 |
Remember that this code could be called every millisecond (depending |
| 313 |
on the output which external command generates), so try to make it |
| 314 |
as lightweight as possible. |
| 315 |
.ie n .IP """discard_output""" 4 |
| 316 |
.el .IP \f(CWdiscard_output\fR 4 |
| 317 |
.IX Item "discard_output" |
| 318 |
Discards the buffering of the standard output and standard errors for return by \fBrun_forked()\fR. |
| 319 |
With this option you have to use the std*_handlers to read what the command outputs. |
| 320 |
Useful for commands that send a lot of output. |
| 321 |
.ie n .IP """terminate_on_parent_sudden_death""" 4 |
| 322 |
.el .IP \f(CWterminate_on_parent_sudden_death\fR 4 |
| 323 |
.IX Item "terminate_on_parent_sudden_death" |
| 324 |
Enable this option if you wish all spawned processes to be killed if the initially spawned |
| 325 |
process (the parent) is killed or dies without waiting for child processes. |
| 326 |
.PP |
| 327 |
\&\f(CW\*(C`run_forked\*(C'\fR will return a HASHREF with the following keys: |
| 328 |
.ie n .IP """exit_code""" 4 |
| 329 |
.el .IP \f(CWexit_code\fR 4 |
| 330 |
.IX Item "exit_code" |
| 331 |
The exit code of the executed program. |
| 332 |
.ie n .IP """timeout""" 4 |
| 333 |
.el .IP \f(CWtimeout\fR 4 |
| 334 |
.IX Item "timeout" |
| 335 |
The number of seconds the program ran for before being terminated, or 0 if no timeout occurred. |
| 336 |
.ie n .IP """stdout""" 4 |
| 337 |
.el .IP \f(CWstdout\fR 4 |
| 338 |
.IX Item "stdout" |
| 339 |
Holds the standard output of the executed command (or empty string if |
| 340 |
there was no STDOUT output or if \f(CW\*(C`discard_output\*(C'\fR was used; it's always defined!) |
| 341 |
.ie n .IP """stderr""" 4 |
| 342 |
.el .IP \f(CWstderr\fR 4 |
| 343 |
.IX Item "stderr" |
| 344 |
Holds the standard error of the executed command (or empty string if |
| 345 |
there was no STDERR output or if \f(CW\*(C`discard_output\*(C'\fR was used; it's always defined!) |
| 346 |
.ie n .IP """merged""" 4 |
| 347 |
.el .IP \f(CWmerged\fR 4 |
| 348 |
.IX Item "merged" |
| 349 |
Holds the standard output and error of the executed command merged into one stream |
| 350 |
(or empty string if there was no output at all or if \f(CW\*(C`discard_output\*(C'\fR was used; it's always defined!) |
| 351 |
.ie n .IP """err_msg""" 4 |
| 352 |
.el .IP \f(CWerr_msg\fR 4 |
| 353 |
.IX Item "err_msg" |
| 354 |
Holds some explanation in the case of an error. |
| 355 |
.ie n .SS "$q = QUOTE" |
| 356 |
.el .SS "\f(CW$q\fP = QUOTE" |
| 357 |
.IX Subsection "$q = QUOTE" |
| 358 |
Returns the character used for quoting strings on this platform. This is |
| 359 |
usually a \f(CW\*(C`\*(Aq\*(C'\fR (single quote) on most systems, but some systems use different |
| 360 |
quotes. For example, \f(CW\*(C`Win32\*(C'\fR uses \f(CW\*(C`"\*(C'\fR (double quote). |
| 361 |
.PP |
| 362 |
You can use it as follows: |
| 363 |
.PP |
| 364 |
.Vb 2 |
| 365 |
\& use IPC::Cmd qw[run QUOTE]; |
| 366 |
\& my $cmd = q[echo ] . QUOTE . q[foo bar] . QUOTE; |
| 367 |
.Ve |
| 368 |
.PP |
| 369 |
This makes sure that \f(CW\*(C`foo bar\*(C'\fR is treated as a string, rather than two |
| 370 |
separate arguments to the \f(CW\*(C`echo\*(C'\fR function. |
| 371 |
.SH "HOW IT WORKS" |
| 372 |
.IX Header "HOW IT WORKS" |
| 373 |
\&\f(CW\*(C`run\*(C'\fR will try to execute your command using the following logic: |
| 374 |
.IP \(bu 4 |
| 375 |
If you have \f(CW\*(C`IPC::Run\*(C'\fR installed, and the variable \f(CW$IPC::Cmd::USE_IPC_RUN\fR |
| 376 |
is set to true (See the "Global Variables" section) use that to execute |
| 377 |
the command. You will have the full output available in buffers, interactive commands |
| 378 |
are sure to work and you are guaranteed to have your verbosity |
| 379 |
settings honored cleanly. |
| 380 |
.IP \(bu 4 |
| 381 |
Otherwise, if the variable \f(CW$IPC::Cmd::USE_IPC_OPEN3\fR is set to true |
| 382 |
(See the "Global Variables" section), try to execute the command using |
| 383 |
IPC::Open3. Buffers will be available on all platforms, |
| 384 |
interactive commands will still execute cleanly, and also your verbosity |
| 385 |
settings will be adhered to nicely; |
| 386 |
.IP \(bu 4 |
| 387 |
Otherwise, if you have the \f(CW\*(C`verbose\*(C'\fR argument set to true, we fall back |
| 388 |
to a simple \f(CWsystem()\fR call. We cannot capture any buffers, but |
| 389 |
interactive commands will still work. |
| 390 |
.IP \(bu 4 |
| 391 |
Otherwise we will try and temporarily redirect STDERR and STDOUT, do a |
| 392 |
\&\f(CWsystem()\fR call with your command and then re-open STDERR and STDOUT. |
| 393 |
This is the method of last resort and will still allow you to execute |
| 394 |
your commands cleanly. However, no buffers will be available. |
| 395 |
.SH "Global Variables" |
| 396 |
.IX Header "Global Variables" |
| 397 |
The behaviour of IPC::Cmd can be altered by changing the following |
| 398 |
global variables: |
| 399 |
.ie n .SS $IPC::Cmd::VERBOSE |
| 400 |
.el .SS \f(CW$IPC::Cmd::VERBOSE\fP |
| 401 |
.IX Subsection "$IPC::Cmd::VERBOSE" |
| 402 |
This controls whether IPC::Cmd will print any output from the |
| 403 |
commands to the screen or not. The default is 0. |
| 404 |
.ie n .SS $IPC::Cmd::USE_IPC_RUN |
| 405 |
.el .SS \f(CW$IPC::Cmd::USE_IPC_RUN\fP |
| 406 |
.IX Subsection "$IPC::Cmd::USE_IPC_RUN" |
| 407 |
This variable controls whether IPC::Cmd will try to use IPC::Run |
| 408 |
when available and suitable. |
| 409 |
.ie n .SS $IPC::Cmd::USE_IPC_OPEN3 |
| 410 |
.el .SS \f(CW$IPC::Cmd::USE_IPC_OPEN3\fP |
| 411 |
.IX Subsection "$IPC::Cmd::USE_IPC_OPEN3" |
| 412 |
This variable controls whether IPC::Cmd will try to use IPC::Open3 |
| 413 |
when available and suitable. Defaults to true. |
| 414 |
.ie n .SS $IPC::Cmd::WARN |
| 415 |
.el .SS \f(CW$IPC::Cmd::WARN\fP |
| 416 |
.IX Subsection "$IPC::Cmd::WARN" |
| 417 |
This variable controls whether run-time warnings should be issued, like |
| 418 |
the failure to load an \f(CW\*(C`IPC::*\*(C'\fR module you explicitly requested. |
| 419 |
.PP |
| 420 |
Defaults to true. Turn this off at your own risk. |
| 421 |
.ie n .SS $IPC::Cmd::INSTANCES |
| 422 |
.el .SS \f(CW$IPC::Cmd::INSTANCES\fP |
| 423 |
.IX Subsection "$IPC::Cmd::INSTANCES" |
| 424 |
This variable controls whether \f(CW\*(C`can_run\*(C'\fR will return all instances of |
| 425 |
the binary it finds in the \f(CW\*(C`PATH\*(C'\fR when called in a list context. |
| 426 |
.PP |
| 427 |
Defaults to false, set to true to enable the described behaviour. |
| 428 |
.ie n .SS $IPC::Cmd::ALLOW_NULL_ARGS |
| 429 |
.el .SS \f(CW$IPC::Cmd::ALLOW_NULL_ARGS\fP |
| 430 |
.IX Subsection "$IPC::Cmd::ALLOW_NULL_ARGS" |
| 431 |
This variable controls whether \f(CW\*(C`run\*(C'\fR will remove any empty/null arguments |
| 432 |
it finds in command arguments. |
| 433 |
.PP |
| 434 |
Defaults to false, so it will remove null arguments. Set to true to allow |
| 435 |
them. |
| 436 |
.SH Caveats |
| 437 |
.IX Header "Caveats" |
| 438 |
.IP "Whitespace and IPC::Open3 / \fBsystem()\fR" 4 |
| 439 |
.IX Item "Whitespace and IPC::Open3 / system()" |
| 440 |
When using \f(CW\*(C`IPC::Open3\*(C'\fR or \f(CW\*(C`system\*(C'\fR, if you provide a string as the |
| 441 |
\&\f(CW\*(C`command\*(C'\fR argument, it is assumed to be appropriately escaped. You can |
| 442 |
use the \f(CW\*(C`QUOTE\*(C'\fR constant to use as a portable quote character (see above). |
| 443 |
However, if you provide an array reference, special rules apply: |
| 444 |
.Sp |
| 445 |
If your command contains \fBspecial characters\fR (< > | &), it will |
| 446 |
be internally stringified before executing the command, to avoid that these |
| 447 |
special characters are escaped and passed as arguments instead of retaining |
| 448 |
their special meaning. |
| 449 |
.Sp |
| 450 |
However, if the command contained arguments that contained whitespace, |
| 451 |
stringifying the command would lose the significance of the whitespace. |
| 452 |
Therefore, \f(CW\*(C`IPC::Cmd\*(C'\fR will quote any arguments containing whitespace in your |
| 453 |
command if the command is passed as an arrayref and contains special characters. |
| 454 |
.IP "Whitespace and IPC::Run" 4 |
| 455 |
.IX Item "Whitespace and IPC::Run" |
| 456 |
When using \f(CW\*(C`IPC::Run\*(C'\fR, if you provide a string as the \f(CW\*(C`command\*(C'\fR argument, |
| 457 |
the string will be split on whitespace to determine the individual elements |
| 458 |
of your command. Although this will usually just Do What You Mean, it may |
| 459 |
break if you have files or commands with whitespace in them. |
| 460 |
.Sp |
| 461 |
If you do not wish this to happen, you should provide an array |
| 462 |
reference, where all parts of your command are already separated out. |
| 463 |
Note however, if there are extra or spurious whitespaces in these parts, |
| 464 |
the parser or underlying code may not interpret it correctly, and |
| 465 |
cause an error. |
| 466 |
.Sp |
| 467 |
Example: |
| 468 |
The following code |
| 469 |
.Sp |
| 470 |
.Vb 1 |
| 471 |
\& gzip \-cdf foo.tar.gz | tar \-xf \- |
| 472 |
.Ve |
| 473 |
.Sp |
| 474 |
should either be passed as |
| 475 |
.Sp |
| 476 |
.Vb 1 |
| 477 |
\& "gzip \-cdf foo.tar.gz | tar \-xf \-" |
| 478 |
.Ve |
| 479 |
.Sp |
| 480 |
or as |
| 481 |
.Sp |
| 482 |
.Vb 1 |
| 483 |
\& [\*(Aqgzip\*(Aq, \*(Aq\-cdf\*(Aq, \*(Aqfoo.tar.gz\*(Aq, \*(Aq|\*(Aq, \*(Aqtar\*(Aq, \*(Aq\-xf\*(Aq, \*(Aq\-\*(Aq] |
| 484 |
.Ve |
| 485 |
.Sp |
| 486 |
But take care not to pass it as, for example |
| 487 |
.Sp |
| 488 |
.Vb 1 |
| 489 |
\& [\*(Aqgzip \-cdf foo.tar.gz\*(Aq, \*(Aq|\*(Aq, \*(Aqtar \-xf \-\*(Aq] |
| 490 |
.Ve |
| 491 |
.Sp |
| 492 |
Since this will lead to issues as described above. |
| 493 |
.IP "IO Redirect" 4 |
| 494 |
.IX Item "IO Redirect" |
| 495 |
Currently it is too complicated to parse your command for IO |
| 496 |
redirections. For capturing STDOUT or STDERR there is a work around |
| 497 |
however, since you can just inspect your buffers for the contents. |
| 498 |
.IP "Interleaving STDOUT/STDERR" 4 |
| 499 |
.IX Item "Interleaving STDOUT/STDERR" |
| 500 |
Neither IPC::Run nor IPC::Open3 can interleave STDOUT and STDERR. For short |
| 501 |
bursts of output from a program, e.g. this sample, |
| 502 |
.Sp |
| 503 |
.Vb 3 |
| 504 |
\& for ( 1..4 ) { |
| 505 |
\& $_ % 2 ? print STDOUT $_ : print STDERR $_; |
| 506 |
\& } |
| 507 |
.Ve |
| 508 |
.Sp |
| 509 |
IPC::[Run|Open3] will first read all of STDOUT, then all of STDERR, meaning |
| 510 |
the output looks like '13' on STDOUT and '24' on STDERR, instead of |
| 511 |
.Sp |
| 512 |
.Vb 4 |
| 513 |
\& 1 |
| 514 |
\& 2 |
| 515 |
\& 3 |
| 516 |
\& 4 |
| 517 |
.Ve |
| 518 |
.Sp |
| 519 |
This has been recorded in rt.cpan.org as bug #37532: Unable to interleave |
| 520 |
STDOUT and STDERR. |
| 521 |
.SH "See Also" |
| 522 |
.IX Header "See Also" |
| 523 |
IPC::Run, IPC::Open3 |
| 524 |
.SH ACKNOWLEDGEMENTS |
| 525 |
.IX Header "ACKNOWLEDGEMENTS" |
| 526 |
Thanks to James Mastros and Martijn van der Streek for their |
| 527 |
help in getting IPC::Open3 to behave nicely. |
| 528 |
.PP |
| 529 |
Thanks to Petya Kohts for the \f(CW\*(C`run_forked\*(C'\fR code. |
| 530 |
.SH "BUG REPORTS" |
| 531 |
.IX Header "BUG REPORTS" |
| 532 |
Please report bugs or other issues to <bug\-ipc\[email protected]>. |
| 533 |
.SH AUTHOR |
| 534 |
.IX Header "AUTHOR" |
| 535 |
Original author: Jos Boumans <[email protected]>. |
| 536 |
Current maintainer: Chris Williams <[email protected]>. |
| 537 |
.SH COPYRIGHT |
| 538 |
.IX Header "COPYRIGHT" |
| 539 |
This library is free software; you may redistribute and/or modify it |
| 540 |
under the same terms as Perl itself. |