[inseparable changes from patch from perl5.003_12 to perl5.003_13]
[p5sagit/p5-mst-13.2.git] / lib / IPC / Open2.pm
CommitLineData
a0d0e21e 1package IPC::Open2;
7e1af8bc 2
3use strict;
4use vars qw($VERSION @ISA @EXPORT);
5
a0d0e21e 6require 5.000;
7require Exporter;
7e1af8bc 8
9$VERSION = 1.01;
10@ISA = qw(Exporter);
11@EXPORT = qw(open2);
a0d0e21e 12
f06db76b 13=head1 NAME
14
15IPC::Open2, open2 - open a process for both reading and writing
16
17=head1 SYNOPSIS
18
19 use IPC::Open2;
cb1a09d0 20 $pid = open2(\*RDR, \*WTR, 'some cmd and args');
f06db76b 21 # or
cb1a09d0 22 $pid = open2(\*RDR, \*WTR, 'some', 'cmd', 'and', 'args');
f06db76b 23
24=head1 DESCRIPTION
25
26The open2() function spawns the given $cmd and connects $rdr for
27reading and $wtr for writing. It's what you think should work
28when you try
29
1fef88e7 30 open(HANDLE, "|cmd args|");
f06db76b 31
7e1af8bc 32If $rdr is a string (that is, a bareword filehandle rather than a glob
33or a reference) and it begins with ">&", then the child will send output
34directly to that file handle. If $wtr is a string that begins with
35"<&", then WTR will be closed in the parent, and the child will read
36from it directly. In both cases, there will be a dup(2) instead of a
37pipe(2) made.
38
f06db76b 39open2() returns the process ID of the child process. It doesn't return on
40failure: it just raises an exception matching C</^open2:/>.
41
42=head1 WARNING
43
44It will not create these file handles for you. You have to do this yourself.
45So don't pass it empty variables expecting them to get filled in for you.
46
47Additionally, this is very dangerous as you may block forever.
48It assumes it's going to talk to something like B<bc>, both writing to
49it and reading from it. This is presumably safe because you "know"
50that commands like B<bc> will read a line at a time and output a line at
51a time. Programs like B<sort> that read their entire input stream first,
52however, are quite apt to cause deadlock.
53
54The big problem with this approach is that if you don't have control
55over source code being run in the the child process, you can't control what it does
cb1a09d0 56with pipe buffering. Thus you can't just open a pipe to C<cat -v> and continually
f06db76b 57read and write a line from it.
58
59=head1 SEE ALSO
60
7e1af8bc 61See L<IPC::Open3> for an alternative that handles STDERR as well. This
62function is really just a wrapper around open3().
f06db76b 63
64=cut
65
a0d0e21e 66# &open2: tom christiansen, <tchrist@convex.com>
67#
68# usage: $pid = open2('rdr', 'wtr', 'some cmd and args');
69# or $pid = open2('rdr', 'wtr', 'some', 'cmd', 'and', 'args');
70#
71# spawn the given $cmd and connect $rdr for
72# reading and $wtr for writing. return pid
73# of child, or 0 on failure.
74#
75# WARNING: this is dangerous, as you may block forever
76# unless you are very careful.
77#
78# $wtr is left unbuffered.
79#
80# abort program if
81# rdr or wtr are null
7e1af8bc 82# a system call fails
a0d0e21e 83
7e1af8bc 84require IPC::Open3;
a0d0e21e 85
86sub open2 {
7e1af8bc 87 my ($read, $write, @cmd) = @_;
88 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
89 return IPC::Open3::_open3('open2', scalar caller,
90 $write, $read, '>&STDERR', @cmd);
a0d0e21e 91}
a0d0e21e 92
7e1af8bc 931