| 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::Open3 3perl" |
| 58 |
.TH IPC::Open3 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::Open3 \- open a process for reading, writing, and error handling using open3() |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 6 |
| 68 |
\& use Symbol \*(Aqgensym\*(Aq; # vivify a separate handle for STDERR |
| 69 |
\& my $pid = open3(my $chld_in, my $chld_out, my $chld_err = gensym, |
| 70 |
\& \*(Aqsome\*(Aq, \*(Aqcmd\*(Aq, \*(Aqand\*(Aq, \*(Aqargs\*(Aq); |
| 71 |
\& # or pass the command through the shell |
| 72 |
\& my $pid = open3(my $chld_in, my $chld_out, my $chld_err = gensym, |
| 73 |
\& \*(Aqsome cmd and args\*(Aq); |
| 74 |
\& |
| 75 |
\& # read from parent STDIN |
| 76 |
\& # send STDOUT and STDERR to already open handle |
| 77 |
\& open my $outfile, \*(Aq>>\*(Aq, \*(Aqoutput.txt\*(Aq or die "open failed: $!"; |
| 78 |
\& my $pid = open3(\*(Aq<&STDIN\*(Aq, $outfile, undef, |
| 79 |
\& \*(Aqsome\*(Aq, \*(Aqcmd\*(Aq, \*(Aqand\*(Aq, \*(Aqargs\*(Aq); |
| 80 |
\& |
| 81 |
\& # write to parent STDOUT and STDERR |
| 82 |
\& my $pid = open3(my $chld_in, \*(Aq>&STDOUT\*(Aq, \*(Aq>&STDERR\*(Aq, |
| 83 |
\& \*(Aqsome\*(Aq, \*(Aqcmd\*(Aq, \*(Aqand\*(Aq, \*(Aqargs\*(Aq); |
| 84 |
\& |
| 85 |
\& # reap zombie and retrieve exit status |
| 86 |
\& waitpid( $pid, 0 ); |
| 87 |
\& my $child_exit_status = $? >> 8; |
| 88 |
.Ve |
| 89 |
.SH DESCRIPTION |
| 90 |
.IX Header "DESCRIPTION" |
| 91 |
Extremely similar to \fBopen2()\fR, \fBopen3()\fR spawns the given command and |
| 92 |
connects \f(CW$chld_out\fR for reading from the child, \f(CW$chld_in\fR for writing to |
| 93 |
the child, and \f(CW$chld_err\fR for errors. If \f(CW$chld_err\fR is false, or the |
| 94 |
same file descriptor as \f(CW$chld_out\fR, then STDOUT and STDERR of the child |
| 95 |
are on the same filehandle. This means that an autovivified lexical |
| 96 |
cannot be used for the STDERR filehandle, but gensym from Symbol can |
| 97 |
be used to vivify a new glob reference, see "SYNOPSIS". The \f(CW$chld_in\fR |
| 98 |
will have autoflush turned on. |
| 99 |
.PP |
| 100 |
If \f(CW$chld_in\fR begins with \f(CW\*(C`<&\*(C'\fR, then \f(CW$chld_in\fR will be closed in the |
| 101 |
parent, and the child will read from it directly. If \f(CW$chld_out\fR or |
| 102 |
\&\f(CW$chld_err\fR begins with \f(CW\*(C`>&\*(C'\fR, then the child will send output |
| 103 |
directly to that filehandle. In both cases, there will be a \fBdup\fR\|(2) |
| 104 |
instead of a \fBpipe\fR\|(2) made. |
| 105 |
.PP |
| 106 |
If either reader or writer is the empty string or undefined, this will |
| 107 |
be replaced by an autogenerated filehandle. If so, you must pass a |
| 108 |
valid lvalue in the parameter slot so it can be overwritten in the |
| 109 |
caller, or an exception will be raised. |
| 110 |
.PP |
| 111 |
The filehandles may also be integers, in which case they are understood |
| 112 |
as file descriptors. |
| 113 |
.PP |
| 114 |
\&\fBopen3()\fR returns the process ID of the child process. It doesn't return on |
| 115 |
failure: it just raises an exception matching \f(CW\*(C`/^open3:/\*(C'\fR. However, |
| 116 |
\&\f(CW\*(C`exec\*(C'\fR failures in the child (such as no such file or permission denied), |
| 117 |
are just reported to \f(CW$chld_err\fR under Windows and OS/2, as it is not possible |
| 118 |
to trap them. |
| 119 |
.PP |
| 120 |
If the child process dies for any reason, the next write to \f(CW$chld_in\fR is |
| 121 |
likely to generate a SIGPIPE in the parent, which is fatal by default. |
| 122 |
So you may wish to handle this signal. |
| 123 |
.PP |
| 124 |
Note if you specify \f(CW\*(C`\-\*(C'\fR as the command, in an analogous fashion to |
| 125 |
\&\f(CW\*(C`open(my $fh, "\-|")\*(C'\fR the child process will just be the forked Perl |
| 126 |
process rather than an external command. This feature isn't yet |
| 127 |
supported on Win32 platforms. |
| 128 |
.PP |
| 129 |
\&\fBopen3()\fR does not wait for and reap the child process after it exits. |
| 130 |
Except for short programs where it's acceptable to let the operating system |
| 131 |
take care of this, you need to do this yourself. This is normally as |
| 132 |
simple as calling \f(CW\*(C`waitpid $pid, 0\*(C'\fR when you're done with the process. |
| 133 |
Failing to do this can result in an accumulation of defunct or "zombie" |
| 134 |
processes. See "waitpid" in perlfunc for more information. |
| 135 |
.PP |
| 136 |
If you try to read from the child's stdout writer and their stderr |
| 137 |
writer, you'll have problems with blocking, which means you'll want |
| 138 |
to use \fBselect()\fR or IO::Select, which means you'd best use |
| 139 |
\&\fBsysread()\fR instead of \fBreadline()\fR for normal stuff. |
| 140 |
.PP |
| 141 |
This is very dangerous, as you may block forever. It assumes it's |
| 142 |
going to talk to something like \fBbc\fR\|(1), both writing to it and reading |
| 143 |
from it. This is presumably safe because you "know" that commands |
| 144 |
like \fBbc\fR\|(1) will read a line at a time and output a line at a time. |
| 145 |
Programs like \fBsort\fR\|(1) that read their entire input stream first, |
| 146 |
however, are quite apt to cause deadlock. |
| 147 |
.PP |
| 148 |
The big problem with this approach is that if you don't have control |
| 149 |
over source code being run in the child process, you can't control |
| 150 |
what it does with pipe buffering. Thus you can't just open a pipe to |
| 151 |
\&\f(CW\*(C`cat \-v\*(C'\fR and continually read and write a line from it. |
| 152 |
.SH "See Also" |
| 153 |
.IX Header "See Also" |
| 154 |
.IP IPC::Open2 4 |
| 155 |
.IX Item "IPC::Open2" |
| 156 |
Like Open3 but without STDERR capture. |
| 157 |
.IP IPC::Run 4 |
| 158 |
.IX Item "IPC::Run" |
| 159 |
This is a CPAN module that has better error handling and more facilities |
| 160 |
than Open3. |
| 161 |
.SH WARNING |
| 162 |
.IX Header "WARNING" |
| 163 |
The order of arguments differs from that of \fBopen2()\fR. |