1IPC::Open3(3pm)        Perl Programmers Reference Guide        IPC::Open3(3pm)


6       IPC::Open3 - open a process for reading, writing, and error handling
7       using open3()


10           use Symbol 'gensym'; # vivify a separate handle for STDERR
11           my $pid = open3(my $chld_in, my $chld_out, my $chld_err = gensym,
12                           'some', 'cmd', 'and', 'args');
13           # or pass the command through the shell
14           my $pid = open3(my $chld_in, my $chld_out, my $chld_err = gensym,
15                           'some cmd and args');
17           # read from parent STDIN
18           # send STDOUT and STDERR to already open handle
19           open my $outfile, '>>', 'output.txt' or die "open failed: $!";
20           my $pid = open3('<&STDIN', $outfile, undef,
21                           'some', 'cmd', 'and', 'args');
23           # write to parent STDOUT and STDERR
24           my $pid = open3(my $chld_in, '>&STDOUT', '>&STDERR',
25                           'some', 'cmd', 'and', 'args');
27           # reap zombie and retrieve exit status
28           waitpid( $pid, 0 );
29           my $child_exit_status = $? >> 8;


32       Extremely similar to open2(), open3() spawns the given command and
33       connects $chld_out for reading from the child, $chld_in for writing to
34       the child, and $chld_err for errors.  If $chld_err is false, or the
35       same file descriptor as $chld_out, then STDOUT and STDERR of the child
36       are on the same filehandle.  This means that an autovivified lexical
37       cannot be used for the STDERR filehandle, but gensym from Symbol can be
38       used to vivify a new glob reference, see "SYNOPSIS".  The $chld_in will
39       have autoflush turned on.
41       If $chld_in begins with "<&", then $chld_in will be closed in the
42       parent, and the child will read from it directly.  If $chld_out or
43       $chld_err begins with ">&", then the child will send output directly to
44       that filehandle.  In both cases, there will be a dup(2) instead of a
45       pipe(2) made.
47       If either reader or writer is the empty string or undefined, this will
48       be replaced by an autogenerated filehandle.  If so, you must pass a
49       valid lvalue in the parameter slot so it can be overwritten in the
50       caller, or an exception will be raised.
52       The filehandles may also be integers, in which case they are understood
53       as file descriptors.
55       open3() returns the process ID of the child process.  It doesn't return
56       on failure: it just raises an exception matching "/^open3:/".  However,
57       "exec" failures in the child (such as no such file or permission
58       denied), are just reported to $chld_err under Windows and OS/2, as it
59       is not possible to trap them.
61       If the child process dies for any reason, the next write to $chld_in is
62       likely to generate a SIGPIPE in the parent, which is fatal by default.
63       So you may wish to handle this signal.
65       Note if you specify "-" as the command, in an analogous fashion to
66       "open(my $fh, "-|")" the child process will just be the forked Perl
67       process rather than an external command.  This feature isn't yet
68       supported on Win32 platforms.
70       open3() does not wait for and reap the child process after it exits.
71       Except for short programs where it's acceptable to let the operating
72       system take care of this, you need to do this yourself.  This is
73       normally as simple as calling "waitpid $pid, 0" when you're done with
74       the process.  Failing to do this can result in an accumulation of
75       defunct or "zombie" processes.  See "waitpid" in perlfunc for more
76       information.
78       If you try to read from the child's stdout writer and their stderr
79       writer, you'll have problems with blocking, which means you'll want to
80       use select() or IO::Select, which means you'd best use sysread()
81       instead of readline() for normal stuff.
83       This is very dangerous, as you may block forever.  It assumes it's
84       going to talk to something like bc(1), both writing to it and reading
85       from it.  This is presumably safe because you "know" that commands like
86       bc(1) will read a line at a time and output a line at a time.  Programs
87       like sort(1) that read their entire input stream first, however, are
88       quite apt to cause deadlock.
90       The big problem with this approach is that if you don't have control
91       over source code being run in the child process, you can't control what
92       it does with pipe buffering.  Thus you can't just open a pipe to "cat
93       -v" and continually read and write a line from it.

See Also

96       IPC::Open2
97           Like Open3 but without STDERR capture.
99       IPC::Run
100           This is a CPAN module that has better error handling and more
101           facilities than Open3.


104       The order of arguments differs from that of open2().
108perl v5.38.2                      2023-11-30                   IPC::Open3(3pm)