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