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