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