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

NAME

6       IPC::Open2, open2 - open a process for both reading and writing
7

SYNOPSIS

9           use IPC::Open2;
10
11           $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'some cmd and args');
12             # or without using the shell
13           $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'some', 'cmd', 'and', 'args');
14
15           # or with handle autovivification
16           my($chld_out, $chld_in);
17           $pid = open2($chld_out, $chld_in, 'some cmd and args');
18             # or without using the shell
19           $pid = open2($chld_out, $chld_in, 'some', 'cmd', 'and', 'args');
20

DESCRIPTION

22       The open2() function runs the given $cmd and connects $chld_out for
23       reading and $chld_in for writing.  It's what you think should work when
24       you try
25
26           $pid = open(HANDLE, "⎪cmd args⎪");
27
28       The write filehandle will have autoflush turned on.
29
30       If $chld_out is a string (that is, a bareword filehandle rather than a
31       glob or a reference) and it begins with ">&", then the child will send
32       output directly to that file handle.  If $chld_in is a string that
33       begins with "<&", then $chld_in will be closed in the parent, and the
34       child will read from it directly.  In both cases, there will be a
35       dup(2) instead of a pipe(2) made.
36
37       If either reader or writer is the null string, this will be replaced by
38       an autogenerated filehandle.  If so, you must pass a valid lvalue in
39       the parameter slot so it can be overwritten in the caller, or an excep‐
40       tion will be raised.
41
42       open2() returns the process ID of the child process.  It doesn't return
43       on failure: it just raises an exception matching "/^open2:/".  However,
44       "exec" failures in the child are not detected.  You'll have to trap
45       SIGPIPE yourself.
46
47       open2() does not wait for and reap the child process after it exits.
48       Except for short programs where it's acceptable to let the operating
49       system take care of this, you need to do this yourself.  This is nor‐
50       mally as simple as calling "waitpid $pid, 0" when you're done with the
51       process.  Failing to do this can result in an accumulation of defunct
52       or "zombie" processes.  See "waitpid" in perlfunc for more information.
53
54       This whole affair is quite dangerous, as you may block forever.  It
55       assumes it's going to talk to something like bc, both writing to it and
56       reading from it.  This is presumably safe because you "know" that com‐
57       mands like bc will read a line at a time and output a line at a time.
58       Programs like sort that read their entire input stream first, however,
59       are quite apt to cause deadlock.
60
61       The big problem with this approach is that if you don't have control
62       over source code being run in the child process, you can't control what
63       it does with pipe buffering.  Thus you can't just open a pipe to "cat
64       -v" and continually read and write a line from it.
65
66       The IO::Pty and Expect modules from CPAN can help with this, as they
67       provide a real tty (well, a pseudo-tty, actually), which gets you back
68       to line buffering in the invoked command again.
69

WARNING

71       The order of arguments differs from that of open3().
72

SEE ALSO

74       See IPC::Open3 for an alternative that handles STDERR as well.  This
75       function is really just a wrapper around open3().
76
77
78
79perl v5.8.8                       2001-09-21                   IPC::Open2(3pm)
Impressum