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