Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / IPC / Open2.html
blob27775b94aa1247f9e3115ecee29ee7baea532b8d
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>open2 - open a process for both reading and writing</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;open2 - open a process for both reading and writing</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#synopsis">SYNOPSIS</a></li>
24 <li><a href="#description">DESCRIPTION</a></li>
25 <li><a href="#warning">WARNING</a></li>
26 <li><a href="#see_also">SEE ALSO</a></li>
27 </ul>
28 <!-- INDEX END -->
30 <hr />
31 <p>
32 </p>
33 <h1><a name="name">NAME</a></h1>
34 <p>IPC::Open2, open2 - open a process for both reading and writing</p>
35 <p>
36 </p>
37 <hr />
38 <h1><a name="synopsis">SYNOPSIS</a></h1>
39 <pre>
40 use IPC::Open2;</pre>
41 <pre>
42 $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'some cmd and args');
43 # or without using the shell
44 $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'some', 'cmd', 'and', 'args');</pre>
45 <pre>
46 # or with handle autovivification
47 my($chld_out, $chld_in);
48 $pid = open2($chld_out, $chld_in, 'some cmd and args');
49 # or without using the shell
50 $pid = open2($chld_out, $chld_in, 'some', 'cmd', 'and', 'args');</pre>
51 <p>
52 </p>
53 <hr />
54 <h1><a name="description">DESCRIPTION</a></h1>
55 <p>The <code>open2()</code> function runs the given $cmd and connects $chld_out for
56 reading and $chld_in for writing. It's what you think should work
57 when you try</p>
58 <pre>
59 $pid = open(HANDLE, &quot;|cmd args|&quot;);</pre>
60 <p>The write filehandle will have autoflush turned on.</p>
61 <p>If $chld_out is a string (that is, a bareword filehandle rather than a glob
62 or a reference) and it begins with <code>&gt;&amp;</code>, then the child will send output
63 directly to that file handle. If $chld_in is a string that begins with
64 <code>&lt;&amp;</code>, then $chld_in will be closed in the parent, and the child will
65 read from it directly. In both cases, there will be a <code>dup(2)</code> instead of a
66 <code>pipe(2)</code> made.</p>
67 <p>If either reader or writer is the null string, this will be replaced
68 by an autogenerated filehandle. If so, you must pass a valid lvalue
69 in the parameter slot so it can be overwritten in the caller, or
70 an exception will be raised.</p>
71 <p><code>open2()</code> returns the process ID of the child process. It doesn't return on
72 failure: it just raises an exception matching <code>/^open2:/</code>. However,
73 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_exec"><code>exec</code></a> failures in the child are not detected. You'll have to
74 trap SIGPIPE yourself.</p>
75 <p><code>open2()</code> does not wait for and reap the child process after it exits.
76 Except for short programs where it's acceptable to let the operating system
77 take care of this, you need to do this yourself. This is normally as
78 simple as calling <code>waitpid $pid, 0</code> when you're done with the process.
79 Failing to do this can result in an accumulation of defunct or ``zombie''
80 processes. See <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#waitpid">waitpid in the perlfunc manpage</a> for more information.</p>
81 <p>This whole affair is quite dangerous, as you may block forever. It
82 assumes it's going to talk to something like <strong>bc</strong>, both writing
83 to it and reading from it. This is presumably safe because you
84 ``know'' that commands like <strong>bc</strong> will read a line at a time and
85 output a line at a time. Programs like <strong>sort</strong> that read their
86 entire input stream first, however, are quite apt to cause deadlock.</p>
87 <p>The big problem with this approach is that if you don't have control
88 over source code being run in the child process, you can't control
89 what it does with pipe buffering. Thus you can't just open a pipe to
90 <code>cat -v</code> and continually read and write a line from it.</p>
91 <p>The IO::Pty and Expect modules from CPAN can help with this, as they
92 provide a real tty (well, a pseudo-tty, actually), which gets you
93 back to line buffering in the invoked command again.</p>
94 <p>
95 </p>
96 <hr />
97 <h1><a name="warning">WARNING</a></h1>
98 <p>The order of arguments differs from that of open3().</p>
99 <p>
100 </p>
101 <hr />
102 <h1><a name="see_also">SEE ALSO</a></h1>
103 <p>See <a href="file://C|\msysgit\mingw\html/lib/IPC/Open3.html">the IPC::Open3 manpage</a> for an alternative that handles STDERR as well. This
104 function is really just a wrapper around open3().</p>
105 <table border="0" width="100%" cellspacing="0" cellpadding="3">
106 <tr><td class="block" style="background-color: #cccccc" valign="middle">
107 <big><strong><span class="block">&nbsp;open2 - open a process for both reading and writing</span></strong></big>
108 </td></tr>
109 </table>
111 </body>
113 </html>