xref: /openbsd-src/gnu/usr.bin/perl/ext/IPC-Open3/lib/IPC/Open2.pm (revision 256a93a44f36679bee503f12e49566c2183f6181)
1package IPC::Open2;
2
3use strict;
4
5require 5.006;
6use Exporter 'import';
7
8our $VERSION	= 1.06;
9our @EXPORT		= qw(open2);
10
11=head1 NAME
12
13IPC::Open2 - open a process for both reading and writing using open2()
14
15=head1 SYNOPSIS
16
17    use IPC::Open2;
18
19    my $pid = open2(my $chld_out, my $chld_in,
20      'some', 'cmd', 'and', 'args');
21    # or passing the command through the shell
22    my $pid = open2(my $chld_out, my $chld_in, 'some cmd and args');
23
24    # read from parent STDIN and write to already open handle
25    open my $outfile, '>', 'outfile.txt' or die "open failed: $!";
26    my $pid = open2($outfile, '<&STDIN', 'some', 'cmd', 'and', 'args');
27
28    # read from already open handle and write to parent STDOUT
29    open my $infile, '<', 'infile.txt' or die "open failed: $!";
30    my $pid = open2('>&STDOUT', $infile, 'some', 'cmd', 'and', 'args');
31
32    # reap zombie and retrieve exit status
33    waitpid( $pid, 0 );
34    my $child_exit_status = $? >> 8;
35
36=head1 DESCRIPTION
37
38The open2() function runs the given command and connects $chld_out for
39reading and $chld_in for writing.  It's what you think should work
40when you try
41
42    my $pid = open(my $fh, "|cmd args|");
43
44The $chld_in filehandle will have autoflush turned on.
45
46If $chld_out is a string (that is, a bareword filehandle rather than a glob
47or a reference) and it begins with C<< >& >>, then the child will send output
48directly to that file handle.  If $chld_in is a string that begins with
49C<< <& >>, then $chld_in will be closed in the parent, and the child will
50read from it directly.  In both cases, there will be a L<dup(2)> instead of a
51L<pipe(2)> made.
52
53If either reader or writer is the empty string or undefined, this will be
54replaced by an autogenerated filehandle.  If so, you must pass a valid lvalue
55in the parameter slot so it can be overwritten in the caller, or
56an exception will be raised.
57
58open2() returns the process ID of the child process.  It doesn't return on
59failure: it just raises an exception matching C</^open2:/>.  However,
60C<exec> failures in the child are not detected.  You'll have to
61trap SIGPIPE yourself.
62
63open2() does not wait for and reap the child process after it exits.
64Except for short programs where it's acceptable to let the operating system
65take care of this, you need to do this yourself.  This is normally as
66simple as calling C<waitpid $pid, 0> when you're done with the process.
67Failing to do this can result in an accumulation of defunct or "zombie"
68processes.  See L<perlfunc/waitpid> for more information.
69
70This whole affair is quite dangerous, as you may block forever.  It
71assumes it's going to talk to something like L<bc(1)>, both writing
72to it and reading from it.  This is presumably safe because you
73"know" that commands like L<bc(1)> will read a line at a time and
74output a line at a time.  Programs like L<sort(1)> that read their
75entire input stream first, however, are quite apt to cause deadlock.
76
77The big problem with this approach is that if you don't have control
78over source code being run in the child process, you can't control
79what it does with pipe buffering.  Thus you can't just open a pipe to
80C<cat -v> and continually read and write a line from it.
81
82The L<IO::Pty> and L<Expect> modules from CPAN can help with this, as
83they provide a real tty (well, a pseudo-tty, actually), which gets you
84back to line buffering in the invoked command again.
85
86=head1 WARNING
87
88The order of arguments differs from that of open3().
89
90=head1 SEE ALSO
91
92See L<IPC::Open3> for an alternative that handles STDERR as well.  This
93function is really just a wrapper around open3().
94
95=cut
96
97# &open2: tom christiansen, <tchrist@convex.com>
98#
99# usage: $pid = open2('rdr', 'wtr', 'some cmd and args');
100#    or  $pid = open2('rdr', 'wtr', 'some', 'cmd', 'and', 'args');
101#
102# spawn the given $cmd and connect $rdr for
103# reading and $wtr for writing.  return pid
104# of child, or 0 on failure.
105#
106# WARNING: this is dangerous, as you may block forever
107# unless you are very careful.
108#
109# $wtr is left unbuffered.
110#
111# abort program if
112#	rdr or wtr are null
113# 	a system call fails
114
115require IPC::Open3;
116
117sub open2 {
118    local $Carp::CarpLevel = $Carp::CarpLevel + 1;
119    return IPC::Open3::_open3('open2', $_[1], $_[0], '>&STDERR', @_[2 .. $#_]);
120}
121
1221
123