1 .\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.10)
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
9 .de Vb \" Begin verbatim text
14 .de Ve \" End verbatim text
18 .\" Set up some character translations and predefined strings. \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote. \*(C+ will
21 .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
29 . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
43 .\" Escape single quotes in literal strings from groff's Unicode transform.
47 .\" If the F register is turned on, we'll generate index entries on stderr for
48 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
49 .\" entries marked with X<> in POD. Of course, you'll have to process the
50 .\" output yourself in some meaningful fashion.
53 . tm Index:\\$1\t\\n%\t"\\$2"
63 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
64 .\" Fear. Run. Save yourself. No user-serviceable parts.
65 . \" fudge factors for nroff and troff
74 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 . \" simple accents for nroff and troff
90 . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
91 . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
92 . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
93 . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
94 . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
95 . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
97 . \" troff and (daisy-wheel) nroff accents
98 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
99 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105 .ds ae a\h'-(\w'a'u*4/10)'e
106 .ds Ae A\h'-(\w'A'u*4/10)'E
107 . \" corrections for vroff
108 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110 . \" for low resolution devices (crt and lpr)
111 .if \n(.H>23 .if \n(.V>19 \
124 .\" ========================================================================
126 .IX Title "IPC::Run3 3"
127 .TH IPC::Run3 3 "2009-05-30" "perl v5.8.7" "User Contributed Perl Documentation"
128 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
129 .\" way too many mistakes in technical documents.
133 IPC::Run3 \- run a subprocess with input/ouput redirection
138 .IX Header "SYNOPSIS"
140 \& use IPC::Run3; # Exports run3() by default
142 \& run3 \e@cmd, \e$in, \e$out, \e$err;
145 .IX Header "DESCRIPTION"
146 This module allows you to run a subprocess and redirect stdin, stdout,
147 and/or stderr to files and perl data structures. It aims to satisfy 99% of the
148 need for using \f(CW\*(C`system\*(C'\fR, \f(CW\*(C`qx\*(C'\fR, and \f(CW\*(C`open3\*(C'\fR
149 with a simple, extremely Perlish \s-1API\s0.
151 Speed, simplicity, and portability are paramount. (That's speed of Perl code;
152 which is often much slower than the kind of buffered I/O that this module uses
153 to spool input to and output from the child command.)
154 .ie n .SS """run3($cmd, $stdin, $stdout, $stderr, \e%options)"""
155 .el .SS "\f(CWrun3($cmd, $stdin, $stdout, $stderr, \e%options)\fP"
156 .IX Subsection "run3($cmd, $stdin, $stdout, $stderr, %options)"
157 All parameters after \f(CW$cmd\fR are optional.
159 The parameters \f(CW$stdin\fR, \f(CW$stdout\fR and \f(CW$stderr\fR indicate
160 how the child's corresponding filehandle
161 (\f(CW\*(C`STDIN\*(C'\fR, \f(CW\*(C`STDOUT\*(C'\fR and \f(CW\*(C`STDERR\*(C'\fR, resp.) will be redirected.
162 Because the redirects come last, this allows \f(CW\*(C`STDOUT\*(C'\fR and \f(CW\*(C`STDERR\*(C'\fR to default
163 to the parent's by just not specifying them \*(-- a common use case.
165 \&\f(CW\*(C`run3\*(C'\fR throws an exception if the wrapped \f(CW\*(C`system\*(C'\fR call returned \-1
166 or anything went wrong with \f(CW\*(C`run3\*(C'\fR's processing of filehandles.
167 Otherwise it returns true.
168 It leaves \f(CW$?\fR intact for inspection of exit and wait status.
170 Note that a true return value from \f(CW\*(C`run3\*(C'\fR doesn't mean that the command
171 had a successful exit code. Hence you should always check \f(CW$?\fR.
173 See \*(L"%options\*(R" for an option to handle the case of \f(CW\*(C`system\*(C'\fR
174 returning \-1 yourself.
177 .IX Subsection "$cmd"
179 Usually \f(CW$cmd\fR will be an \s-1ARRAY\s0 reference and the child is invoked via
185 But \f(CW$cmd\fR may also be a string in which case the child is invoked via
191 (cf. \*(L"system\*(R" in perlfunc for the difference and the pitfalls of using
194 \fI\f(CI$stdin\fI, \f(CI$stdout\fI, \f(CI$stderr\fI\fR
195 .IX Subsection "$stdin, $stdout, $stderr"
197 The parameters \f(CW$stdin\fR, \f(CW$stdout\fR and \f(CW$stderr\fR
198 can take one of the following forms:
199 .ie n .IP """undef"" (or not specified at all)" 4
200 .el .IP "\f(CWundef\fR (or not specified at all)" 4
201 .IX Item "undef (or not specified at all)"
202 The child inherits the corresponding filehandle from the parent.
205 \& run3 \e@cmd, $stdin; # child writes to same STDOUT and STDERR as parent
206 \& run3 \e@cmd, undef, $stdout, $stderr; # child reads from same STDIN as parent
208 .ie n .IP """\eundef""" 4
209 .el .IP "\f(CW\eundef\fR" 4
211 The child's filehandle is redirected from or to the
212 local equivalent of \f(CW\*(C`/dev/null\*(C'\fR (as returned by
213 \&\f(CW\*(C`File::Spec\->devnull()\*(C'\fR).
216 \& run3 \e@cmd, \eundef, $stdout, $stderr; # child reads from /dev/null
218 .IP "a simple scalar" 4
219 .IX Item "a simple scalar"
220 The parameter is taken to be the name of a file to read from
221 or write to. In the latter case, the file will be opened via
227 i.e. it is created if it doesn't exist and truncated otherwise.
228 Note that the file is opened by the parent which will croak
232 \& run3 \e@cmd, \eundef, "out.txt"; # child writes to file "out.txt"
234 .ie n .IP "a filehandle (either a reference to a \s-1GLOB\s0 or an ""IO::Handle"")" 4
235 .el .IP "a filehandle (either a reference to a \s-1GLOB\s0 or an \f(CWIO::Handle\fR)" 4
236 .IX Item "a filehandle (either a reference to a GLOB or an IO::Handle)"
237 The filehandle is inherited by the child.
240 \& open my $fh, ">", "out.txt";
241 \& print $fh "prologue\en";
243 \& run3 \e@cmd, \eundef, $fh; # child writes to $fh
245 \& print $fh "epilogue\en";
248 .IP "a \s-1SCALAR\s0 reference" 4
249 .IX Item "a SCALAR reference"
250 The referenced scalar is treated as a string to be read from or
251 written to. In the latter case, the previous content of the string
256 \& run3 \e@cmd, \eundef, \e$out; # child writes into string
257 \& run3 \e@cmd, \e<<EOF; # child reads from string (can use "here" notation)
263 .IP "an \s-1ARRAY\s0 reference" 4
264 .IX Item "an ARRAY reference"
265 For \f(CW$stdin\fR, the elements of \f(CW@$stdin\fR are simply spooled to the child.
267 For \f(CW$stdout\fR or \f(CW$stderr\fR, the child's corresponding file descriptor
268 is read line by line (as determined by the current setting of \f(CW$/\fR)
269 into \f(CW@$stdout\fR or \f(CW@$stderr\fR, resp. The previous content of the array
274 \& run3 \e@cmd, \eundef, \e@lines; # child writes into array
276 .IP "a \s-1CODE\s0 reference" 4
277 .IX Item "a CODE reference"
278 For \f(CW$stdin\fR, \f(CW&$stdin\fR will be called repeatedly (with no arguments) and
279 the return values are spooled to the child. \f(CW&$stdin\fR must signal the end of
280 input by returning \f(CW\*(C`undef\*(C'\fR.
282 For \f(CW$stdout\fR or \f(CW$stderr\fR, the child's corresponding file descriptor
283 is read line by line (as determined by the current setting of \f(CW$/\fR)
284 and \f(CW&$stdout\fR or \f(CW&$stderr\fR, resp., is called with the contents of the line.
285 Note that there's no end-of-file indication.
290 \& return $i < 10 ? "line".$i++."\en" : undef;
293 \& run3 \e@cmd, \e&producer; # child reads 10 lines
296 Note that this form of redirecting the child's I/O doesn't imply
297 any form of concurrency between parent and child \- \fIrun3()\fR's method of
298 operation is the same no matter which form of redirection you specify.
300 If the same value is passed for \f(CW$stdout\fR and \f(CW$stderr\fR, then the child
301 will write both \f(CW\*(C`STDOUT\*(C'\fR and \f(CW\*(C`STDERR\*(C'\fR to the same filehandle.
302 In general, this means that
305 \& run3 \e@cmd, \eundef, "foo.txt", "foo.txt";
306 \& run3 \e@cmd, \eundef, \e$both, \e$both;
309 will \s-1DWIM\s0 and pass a single file handle to the child for both \f(CW\*(C`STDOUT\*(C'\fR and
310 \&\f(CW\*(C`STDERR\*(C'\fR, collecting all into file \*(L"foo.txt\*(R" or \f(CW$both\fR.
312 \fI\f(CI\*(C`\e%options\*(C'\fI\fR
313 .IX Subsection "%options"
315 The last parameter, \f(CW\*(C`\e%options\*(C'\fR, must be a hash reference if present.
317 Currently the following
319 .ie n .IP """binmode_stdin"", ""binmode_stdout"", ""binmode_stderr""" 4
320 .el .IP "\f(CWbinmode_stdin\fR, \f(CWbinmode_stdout\fR, \f(CWbinmode_stderr\fR" 4
321 .IX Item "binmode_stdin, binmode_stdout, binmode_stderr"
322 The value must a \*(L"layer\*(R" as described in \*(L"binmode\*(R" in perlfunc.
323 If specified the corresponding
324 parameter \f(CW$stdin\fR, \f(CW$stdout\fR or \f(CW$stderr\fR, resp., operates
325 with the given layer.
327 For backward compatibility, a true value that doesn't start with \*(L":\*(R"
328 (e.g. a number) is interpreted as \*(L":raw\*(R". If the value is false
329 or not specified, the default is \*(L":crlf\*(R" on Windows and \*(L":raw\*(R" otherwise.
331 Don't expect that values other than the built-in layers \*(L":raw\*(R", \*(L":crlf\*(R",
332 and (on newer Perls) \*(L":bytes\*(R", \*(L":utf8\*(R", \*(L":encoding(...)\*(R" will work.
333 .ie n .IP """append_stdout"", ""append_stderr""" 4
334 .el .IP "\f(CWappend_stdout\fR, \f(CWappend_stderr\fR" 4
335 .IX Item "append_stdout, append_stderr"
336 If their value is true then the corresponding
337 parameter \f(CW$stdout\fR or \f(CW$stderr\fR, resp., will append the child's output
338 to the existing \*(L"contents\*(R" of the redirector. This only makes
339 sense if the redirector is a simple scalar (the corresponding file
340 is opened in append mode), a \s-1SCALAR\s0 reference (the output is
341 appended to the previous contents of the string)
342 or an \s-1ARRAY\s0 reference (the output is \f(CW\*(C`push\*(C'\fRed onto the
343 previous contents of the array).
344 .ie n .IP """return_if_system_error""" 4
345 .el .IP "\f(CWreturn_if_system_error\fR" 4
346 .IX Item "return_if_system_error"
347 If this is true \f(CW\*(C`run3\*(C'\fR does \fBnot\fR throw an exception if \f(CW\*(C`system\*(C'\fR
348 returns \-1 (cf. \*(L"system\*(R" in perlfunc for possible
349 failure scenarios.), but returns true instead.
350 In this case \f(CW$?\fR has the value \-1 and \f(CW$!\fR
351 contains the errno of the failing \f(CW\*(C`system\*(C'\fR call.
353 .IX Header "HOW IT WORKS"
356 For each redirector \f(CW$stdin\fR, \f(CW$stdout\fR, and \f(CW$stderr\fR,
357 \&\f(CW\*(C`run3()\*(C'\fR furnishes a filehandle:
360 if the redirector already specifies a filehandle it just uses that
362 if the redirector specifies a filename, \f(CW\*(C`run3()\*(C'\fR opens the file
363 in the appropriate mode
365 in all other cases, \f(CW\*(C`run3()\*(C'\fR opens a temporary file
372 If \f(CW\*(C`run3()\*(C'\fR opened a temporary file for \f(CW$stdin\fR in step (1),
373 it writes the data using the specified method (either
374 from a string, an array or returnd by a function) to the temporary file and rewinds it.
377 \&\f(CW\*(C`run3()\*(C'\fR saves the parent's \f(CW\*(C`STDIN\*(C'\fR, \f(CW\*(C`STDOUT\*(C'\fR and \f(CW\*(C`STDERR\*(C'\fR by duplicating
378 them to new filehandles. It duplicates the filehandles from step (1)
379 to \f(CW\*(C`STDIN\*(C'\fR, \f(CW\*(C`STDOUT\*(C'\fR and \f(CW\*(C`STDERR\*(C'\fR, resp.
382 \&\f(CW\*(C`run3()\*(C'\fR runs the child by invoking system
383 with \f(CW$cmd\fR as specified above.
386 \&\f(CW\*(C`run3()\*(C'\fR restores the parent's \f(CW\*(C`STDIN\*(C'\fR, \f(CW\*(C`STDOUT\*(C'\fR and \f(CW\*(C`STDERR\*(C'\fR saved in step (3).
389 If \f(CW\*(C`run3()\*(C'\fR opened a temporary file for \f(CW$stdout\fR or \f(CW$stderr\fR in step (1),
390 it rewinds it and reads back its contents using the specified method
391 (either to a string, an array or by calling a function).
394 \&\f(CW\*(C`run3()\*(C'\fR closes all filehandles that it opened explicitly in step (1).
396 Note that when using temporary files, \f(CW\*(C`run3()\*(C'\fR tries to amortize the overhead
397 by reusing them (i.e. it keeps them open and rewinds and truncates them
398 before the next operation).
400 .IX Header "LIMITATIONS"
401 Often uses intermediate files (determined by File::Temp, and thus by the
402 File::Spec defaults and the \s-1TMPDIR\s0 env. variable) for speed, portability and
405 Use extrem caution when using \f(CW\*(C`run3\*(C'\fR in a threaded environment if
406 concurrent calls of \f(CW\*(C`run3\*(C'\fR are possible. Most likely, I/O from different
407 invocations will get mixed up. The reason is that in most thread
408 implementations all threads in a process share the same \s-1STDIN/STDOUT/STDERR\s0.
409 Known failures are Perl ithreads on Linux and Win32. Note that \f(CW\*(C`fork\*(C'\fR
410 on Win32 is emulated via Win32 threads and hence I/O mix up is possible
411 between forked children here (\f(CW\*(C`run3\*(C'\fR is \*(L"fork safe\*(R" on Unix, though).
413 .IX Header "DEBUGGING"
414 To enable debugging use the \s-1IPCRUN3DEBUG\s0 environment variable to
415 a non-zero integer value:
418 \& $ IPCRUN3DEBUG=1 myapp
421 .IX Header "PROFILING"
422 To enable profiling, set \s-1IPCRUN3PROFILE\s0 to a number to enable emitting profile
423 information to \s-1STDERR\s0 (1 to get timestamps, 2 to get a summary report at the
424 \&\s-1END\s0 of the program, 3 to get mini reports after each run) or to a filename to
425 emit raw data to a file for later analysis.
427 .IX Header "COMPARISON"
428 Here's how it stacks up to existing APIs:
429 .ie n .SS "compared to ""system()"", ""qx\*(Aq\*(Aq"", ""open ""...|"""", ""open ""|..."""""
430 .el .SS "compared to \f(CWsystem()\fP, \f(CWqx\*(Aq\*(Aq\fP, \f(CWopen ``...|''\fP, \f(CWopen ``|...''\fP"
431 .IX Subsection "compared to system(), qx, open ""...|"", open ""|..."""
433 redirects more than one file descriptor
435 returns \s-1TRUE\s0 on success, \s-1FALSE\s0 on failure
437 throws an error if problems occur in the parent process (or the pre-exec child)
439 allows a very perlish interface to Perl data structures and subroutines
441 allows 1 word invocations to avoid the shell easily:
444 \& run3 ["foo"]; # does not invoke shell
447 does not return the exit code, leaves it in $?
448 .ie n .SS "compared to ""open2()"", ""open3()"""
449 .el .SS "compared to \f(CWopen2()\fP, \f(CWopen3()\fP"
450 .IX Subsection "compared to open2(), open3()"
452 no lengthy, error prone polling/select loop needed
454 hides \s-1OS\s0 dependancies
456 allows \s-1SCALAR\s0, \s-1ARRAY\s0, and \s-1CODE\s0 references to source and sink I/O
458 I/O parameter order is like \f(CW\*(C`open3()\*(C'\fR (not like \f(CW\*(C`open2()\*(C'\fR).
460 does not allow interaction with the subprocess
461 .SS "compared to \fIIPC::Run::run()\fP"
462 .IX Subsection "compared to IPC::Run::run()"
464 smaller, lower overhead, simpler, more portable
466 no \fIselect()\fR loop portability issues
468 does not fall prey to Perl closure leaks
470 does not allow interaction with the subprocess (which
471 \&\fIIPC::Run::run()\fR allows by redirecting subroutines)
473 lacks many features of \f(CW\*(C`IPC::Run::run()\*(C'\fR (filters, pipes,
474 redirects, pty support)
476 .IX Header "COPYRIGHT"
477 Copyright 2003, R. Barrie Slaymaker, Jr., All Rights Reserved
480 You may use this module under the terms of the \s-1BSD\s0, Artistic, or \s-1GPL\s0 licenses,
484 Barrie Slaymaker <\f(CW\*(C`barries@slaysys.com\*(C'\fR>
486 Ricardo \s-1SIGNES\s0 <\f(CW\*(C`rjbs@cpan.org\*(C'\fR> performed some routine maintenance in
487 2005, thanks to help from the following ticket and/or patch submitters: Jody
488 Belka, Roderich Schupp, David Morel, and anonymous others.