1IPC::Open3(3pm) Perl Programmers Reference Guide IPC::Open3(3pm)
2
3
4
6 IPC::Open3 - open a process for reading, writing, and error handling
7 using open3()
8
10 $pid = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR,
11 'some cmd and args', 'optarg', ...);
12
13 my($wtr, $rdr, $err);
14 use Symbol 'gensym'; $err = gensym;
15 $pid = open3($wtr, $rdr, $err,
16 'some cmd and args', 'optarg', ...);
17
18 waitpid( $pid, 0 );
19 my $child_exit_status = $? >> 8;
20
22 Extremely similar to open2(), open3() spawns the given $cmd and
23 connects CHLD_OUT for reading from the child, CHLD_IN for writing to
24 the child, and CHLD_ERR for errors. If CHLD_ERR is false, or the same
25 file descriptor as CHLD_OUT, then STDOUT and STDERR of the child are on
26 the same filehandle (this means that an autovivified lexical cannot be
27 used for the STDERR filehandle, see SYNOPSIS). The CHLD_IN will have
28 autoflush turned on.
29
30 If CHLD_IN begins with "<&", then CHLD_IN will be closed in the parent,
31 and the child will read from it directly. If CHLD_OUT or CHLD_ERR
32 begins with ">&", then the child will send output directly to that
33 filehandle. In both cases, there will be a dup(2) instead of a pipe(2)
34 made.
35
36 If either reader or writer is the null string, this will be replaced by
37 an autogenerated filehandle. If so, you must pass a valid lvalue in
38 the parameter slot so it can be overwritten in the caller, or an
39 exception will be raised.
40
41 The filehandles may also be integers, in which case they are understood
42 as file descriptors.
43
44 open3() returns the process ID of the child process. It doesn't return
45 on failure: it just raises an exception matching "/^open3:/". However,
46 "exec" failures in the child (such as no such file or permission
47 denied), are just reported to CHLD_ERR under Windows and OS/2, as it is
48 not possible to trap them.
49
50 If the child process dies for any reason, the next write to CHLD_IN is
51 likely to generate a SIGPIPE in the parent, which is fatal by default.
52 So you may wish to handle this signal.
53
54 Note if you specify "-" as the command, in an analogous fashion to
55 "open(FOO, "-|")" the child process will just be the forked Perl
56 process rather than an external command. This feature isn't yet
57 supported on Win32 platforms.
58
59 open3() does not wait for and reap the child process after it exits.
60 Except for short programs where it's acceptable to let the operating
61 system take care of this, you need to do this yourself. This is
62 normally as simple as calling "waitpid $pid, 0" when you're done with
63 the process. Failing to do this can result in an accumulation of
64 defunct or "zombie" processes. See "waitpid" in perlfunc for more
65 information.
66
67 If you try to read from the child's stdout writer and their stderr
68 writer, you'll have problems with blocking, which means you'll want to
69 use select() or the IO::Select, which means you'd best use sysread()
70 instead of readline() for normal stuff.
71
72 This is very dangerous, as you may block forever. It assumes it's
73 going to talk to something like bc, both writing to it and reading from
74 it. This is presumably safe because you "know" that commands like bc
75 will read a line at a time and output a line at a time. Programs like
76 sort that read their entire input stream first, however, are quite apt
77 to cause deadlock.
78
79 The big problem with this approach is that if you don't have control
80 over source code being run in the child process, you can't control what
81 it does with pipe buffering. Thus you can't just open a pipe to "cat
82 -v" and continually read and write a line from it.
83
85 IPC::Open2
86 Like Open3 but without STDERR capture.
87
88 IPC::Run
89 This is a CPAN module that has better error handling and more
90 facilities than Open3.
91
93 The order of arguments differs from that of open2().
94
95
96
97perl v5.28.2 2018-03-01 IPC::Open3(3pm)