home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_ef496d123e1fc8e68ae09bbd5dd159b5
< prev
next >
Wrap
Text File
|
2000-03-23
|
5KB
|
116 lines
<HTML>
<HEAD>
<TITLE>open2 - open a process for both reading and writing</TITLE>
<LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> open2 - open a process for both reading and writing</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<LI><A HREF="#warning">WARNING</A></LI>
<LI><A HREF="#see also">SEE ALSO</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>IPC::Open2, open2 - open a process for both reading and writing</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use IPC::Open2;</PRE>
<PRE>
$pid = open2(\*RDRFH, \*WTRFH, 'some cmd and args');
# or without using the shell
$pid = open2(\*RDRFH, \*WTRFH, 'some', 'cmd', 'and', 'args');</PRE>
<PRE>
# or with handle autovivification
my($rdrfh, $wtrfh);
$pid = open2($rdrfh, $wtrfh, 'some cmd and args');
# or without using the shell
$pid = open2($rdrfh, $wtrfh, 'some', 'cmd', 'and', 'args');</PRE>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>The <CODE>open2()</CODE> function runs the given $cmd and connects $rdrfh for
reading and $wtrfh for writing. It's what you think should work
when you try</P>
<PRE>
$pid = open(HANDLE, "|cmd args|");</PRE>
<P>The write filehandle will have autoflush turned on.</P>
<P>If $rdrfh is a string (that is, a bareword filehandle rather than a glob
or a reference) and it begins with <CODE>>&</CODE>, then the child will send output
directly to that file handle. If $wtrfh is a string that begins with
<CODE><&</CODE>, then $wtrfh will be closed in the parent, and the child will read
from it directly. 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><CODE>open2()</CODE> returns the process ID of the child process. It doesn't return on
failure: it just raises an exception matching <CODE>/^open2:/</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><CODE>open2()</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 ``zombie''
processes. See <A HREF="../../lib/Pod/perlfunc.html#waitpid">waitpid in the perlfunc manpage</A> for more information.</P>
<P>This whole affair is quite 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
``know'' 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>The IO::Pty and Expect modules from CPAN can help with this, as they
provide a real tty (well, a pseudo-tty, actually), which gets you
back to line buffering in the invoked command again.</P>
<P>
<HR>
<H1><A NAME="warning">WARNING</A></H1>
<P>The order of arguments differs from that of open3().</P>
<P>
<HR>
<H1><A NAME="see also">SEE ALSO</A></H1>
<P>See <A HREF="../../lib/IPC/Open3.html">the IPC::Open3 manpage</A> for an alternative that handles STDERR as well. This
function is really just a wrapper around open3().</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> open2 - open a process for both reading and writing</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>