open3.html

来自「perl教程」· HTML 代码 · 共 109 行

HTML
109
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>open3 - open a process for reading, writing, and error handling</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>open3 - open a process for reading, writing, and error handling</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#warning">WARNING</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>IPC::Open3, open3 - open a process for reading, writing, and error handling</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
    <span class="variable">$pid</span> <span class="operator">=</span> <span class="variable">open3</span><span class="operator">(\</span><span class="variable">*CHLD_IN</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">*CHLD_OUT</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">*CHLD_ERR</span><span class="operator">,</span>
                    <span class="string">'some cmd and args'</span><span class="operator">,</span> <span class="string">'optarg'</span><span class="operator">,</span> <span class="operator">...);</span>
</pre>
<pre>
    <span class="keyword">my</span><span class="operator">(</span><span class="variable">$wtr</span><span class="operator">,</span> <span class="variable">$rdr</span><span class="operator">,</span> <span class="variable">$err</span><span class="operator">);</span>
    <span class="variable">$pid</span> <span class="operator">=</span> <span class="variable">open3</span><span class="operator">(</span><span class="variable">$wtr</span><span class="operator">,</span> <span class="variable">$rdr</span><span class="operator">,</span> <span class="variable">$err</span><span class="operator">,</span>
                    <span class="string">'some cmd and args'</span><span class="operator">,</span> <span class="string">'optarg'</span><span class="operator">,</span> <span class="operator">...);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Extremely similar to open2(), <code>open3()</code> spawns the given $cmd and
connects CHLD_OUT for reading from the child, CHLD_IN for writing to
the child, and CHLD_ERR for errors.  If CHLD_ERR is false, or the
same file descriptor as CHLD_OUT, then STDOUT and STDERR of the child
are on the same filehandle.  The CHLD_IN will have autoflush turned
on.</p>
<p>If CHLD_IN begins with <code>&lt;&amp;</code>, then CHLD_IN will be closed in the
parent, and the child will read from it directly.  If CHLD_OUT or
CHLD_ERR begins with <code>&gt;&amp;</code>, then the child will send output
directly to that filehandle.  In both cases, there will be a <code>dup(2)</code>
instead of a <a href="../../lib/Pod/perlfunc.html#item_pipe"><code>pipe(2)</code></a> made.</p>
<p>If either reader or writer is the null string, this will be replaced
by an autogenerated filehandle.  If so, you must pass a valid lvalue
in the parameter slot so it can be overwritten in the caller, or 
an exception will be raised.</p>
<p>The filehandles may also be integers, in which case they are understood
as file descriptors.</p>
<p><code>open3()</code> returns the process ID of the child process.  It doesn't return on
failure: it just raises an exception matching <code>/^open3:/</code>.  However,
<a href="../../lib/Pod/perlfunc.html#item_exec"><code>exec</code></a> failures in the child are not detected.  You'll have to 
trap SIGPIPE yourself.</p>
<p>Note if you specify <code>-</code> as the command, in an analogous fashion to
<a href="../../lib/Pod/perlfunc.html#item_open"><code>open(FOO, &quot;-|&quot;)</code></a> the child process will just be the forked Perl
process rather than an external command.  This feature isn't yet
supported on Win32 platforms.</p>
<p><code>open3()</code> does not wait for and reap the child process after it exits.  
Except for short programs where it's acceptable to let the operating system
take care of this, you need to do this yourself.  This is normally as 
simple as calling <code>waitpid $pid, 0</code> when you're done with the process.
Failing to do this can result in an accumulation of defunct or &quot;zombie&quot;
processes.  See <a href="../../lib/Pod/perlfunc.html#waitpid">waitpid in the perlfunc manpage</a> for more information.</p>
<p>If you try to read from the child's stdout writer and their stderr
writer, you'll have problems with blocking, which means you'll want
to use <a href="../../lib/Pod/perlfunc.html#item_select"><code>select()</code></a> or the IO::Select, which means you'd best use
<a href="../../lib/Pod/perlfunc.html#item_sysread"><code>sysread()</code></a> instead of <a href="../../lib/Pod/perlfunc.html#item_readline"><code>readline()</code></a> for normal stuff.</p>
<p>This is very dangerous, as you may block forever.  It assumes it's
going to talk to something like <strong>bc</strong>, both writing to it and reading
from it.  This is presumably safe because you &quot;know&quot; that commands
like <strong>bc</strong> will read a line at a time and output a line at a time.
Programs like <strong>sort</strong> that read their entire input stream first,
however, are quite apt to cause deadlock.</p>
<p>The big problem with this approach is that if you don't have control
over source code being run in the child process, you can't control
what it does with pipe buffering.  Thus you can't just open a pipe to
<code>cat -v</code> and continually read and write a line from it.</p>
<p>
</p>
<hr />
<h1><a name="warning">WARNING</a></h1>
<p>The order of arguments differs from that of open2().</p>

</body>

</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?