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

NAME

6       IPC::Open2 - open a process for both reading and writing using open2()
7

SYNOPSIS

9           use IPC::Open2;
10
11           my $pid = open2(my $chld_out, my $chld_in,
12             'some', 'cmd', 'and', 'args');
13           # or passing the command through the shell
14           my $pid = open2(my $chld_out, my $chld_in, 'some cmd and args');
15
16           # read from parent STDIN and write to already open handle
17           open my $outfile, '>', 'outfile.txt' or die "open failed: $!";
18           my $pid = open2($outfile, '<&STDIN', 'some', 'cmd', 'and', 'args');
19
20           # read from already open handle and write to parent STDOUT
21           open my $infile, '<', 'infile.txt' or die "open failed: $!";
22           my $pid = open2('>&STDOUT', $infile, 'some', 'cmd', 'and', 'args');
23
24           # reap zombie and retrieve exit status
25           waitpid( $pid, 0 );
26           my $child_exit_status = $? >> 8;
27

DESCRIPTION

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

WARNING

79       The order of arguments differs from that of open3().
80

SEE ALSO

82       See IPC::Open3 for an alternative that handles STDERR as well.  This
83       function is really just a wrapper around open3().
84
85
86
87perl v5.36.0                      2022-08-30                   IPC::Open2(3pm)
Impressum