Perl 5.001
[p5sagit/p5-mst-13.2.git] / pod / modpods / Open2.pod
CommitLineData
a0d0e21e 1=head1 NAME
2
3IPC::Open2, open2 - open a process for both reading and writing
4
5=head1 SYNOPSIS
6
7 use IPC::Open2;
8 $pid = open2('rdr', 'wtr', 'some cmd and args');
9 # or
10 $pid = open2('rdr', 'wtr', 'some', 'cmd', 'and', 'args');
11
12=head1 DESCRIPTION
13
14The open2() function spawns the given $cmd and connects $rdr for
15reading and $wtr for writing. It's what you think should work
16when you try
17
18 open(HANDLE, "|cmd args");
19
20open2() returns the process ID of the child process. It doesn't return on
21failure: it just raises an exception matching C</^open2:/>.
22
23=head1 WARNING
24
25It will not create these file handles for you. You have to do this yourself.
26So don't pass it empty variables expecting them to get filled in for you.
27
28Additionally, this is very dangerous as you may block forever.
29It assumes it's going to talk to something like B<bc>, both writing to
30it and reading from it. This is presumably safe because you "know"
31that commands like B<bc> will read a line at a time and output a line at
32a time. Programs like B<sort> that read their entire input stream first,
748a9306 33however, are quite apt to cause deadlock.
34
35The big problem with this approach is that if you don't have control
36over source code being run in the the child process, you can't control what it does
37with pipe buffering. Thus you can't just open a pipe to "cat -v" and continually
38read and write a line from it.
39
40=head1 SEE ALSO
41
42See L<open3> for an alternative that handles STDERR as well.
43