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

NAME

6       IPC::Open3, open3 - open a process for reading, writing, and error han‐
7       dling
8

SYNOPSIS

10           $pid = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR,
11                           'some cmd and args', 'optarg', ...);
12
13           my($wtr, $rdr, $err);
14           $pid = open3($wtr, $rdr, $err,
15                           'some cmd and args', 'optarg', ...);
16

DESCRIPTION

18       Extremely similar to open2(), open3() spawns the given $cmd and con‐
19       nects CHLD_OUT for reading from the child, CHLD_IN for writing to the
20       child, and CHLD_ERR for errors.  If CHLD_ERR is false, or the same file
21       descriptor as CHLD_OUT, then STDOUT and STDERR of the child are on the
22       same filehandle.  The CHLD_IN will have autoflush turned on.
23
24       If CHLD_IN begins with "<&", then CHLD_IN will be closed in the parent,
25       and the child will read from it directly.  If CHLD_OUT or CHLD_ERR
26       begins with ">&", then the child will send output directly to that
27       filehandle.  In both cases, there will be a dup(2) instead of a pipe(2)
28       made.
29
30       If either reader or writer is the null string, this will be replaced by
31       an autogenerated filehandle.  If so, you must pass a valid lvalue in
32       the parameter slot so it can be overwritten in the caller, or an excep‐
33       tion will be raised.
34
35       The filehandles may also be integers, in which case they are understood
36       as file descriptors.
37
38       open3() returns the process ID of the child process.  It doesn't return
39       on failure: it just raises an exception matching "/^open3:/".  However,
40       "exec" failures in the child are not detected.  You'll have to trap
41       SIGPIPE yourself.
42
43       Note if you specify "-" as the command, in an analogous fashion to
44       "open(FOO, "-⎪")" the child process will just be the forked Perl
45       process rather than an external command.  This feature isn't yet sup‐
46       ported on Win32 platforms.
47
48       open3() does not wait for and reap the child process after it exits.
49       Except for short programs where it's acceptable to let the operating
50       system take care of this, you need to do this yourself.  This is nor‐
51       mally as simple as calling "waitpid $pid, 0" when you're done with the
52       process.  Failing to do this can result in an accumulation of defunct
53       or "zombie" processes.  See "waitpid" in perlfunc for more information.
54
55       If you try to read from the child's stdout writer and their stderr
56       writer, you'll have problems with blocking, which means you'll want to
57       use select() or the IO::Select, which means you'd best use sysread()
58       instead of readline() for normal stuff.
59
60       This is very dangerous, as you may block forever.  It assumes it's
61       going to talk to something like bc, both writing to it and reading from
62       it.  This is presumably safe because you "know" that commands like bc
63       will read a line at a time and output a line at a time.  Programs like
64       sort that read their entire input stream first, however, are quite apt
65       to cause deadlock.
66
67       The big problem with this approach is that if you don't have control
68       over source code being run in the child process, you can't control what
69       it does with pipe buffering.  Thus you can't just open a pipe to "cat
70       -v" and continually read and write a line from it.
71

WARNING

73       The order of arguments differs from that of open2().
74
75
76
77perl v5.8.8                       2001-09-21                   IPC::Open3(3pm)
Impressum