📄 ch16_09.htm
字号:
<HTML><HEAD><TITLE>Recipe 16.8. Controlling Input and Output of Another Program (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:43:51Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch16_01.htm"TITLE="16. Process Management and Communication"><LINKREL="prev"HREF="ch16_08.htm"TITLE="16.7. Reading STDERR from a Program"><LINKREL="next"HREF="ch16_10.htm"TITLE="16.9. Controlling the Input, Output, and Error of Another Program"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_08.htm"TITLE="16.7. Reading STDERR from a Program"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.7. Reading STDERR from a Program"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch16_01.htm"TITLE="16. Process Management and Communication"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_10.htm"TITLE="16.9. Controlling the Input, Output, and Error of Another Program"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.9. Controlling the Input, Output, and Error of Another Program"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch16-17010">16.8. Controlling Input and Output of Another Program</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1394">Problem<ACLASS="indexterm"NAME="ch16-idx-1000006314-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006314-1"></A><ACLASS="indexterm"NAME="ch16-idx-1000006314-2"></A><ACLASS="indexterm"NAME="ch16-idx-1000006314-3"></A></A></H3><PCLASS="para">You want to both write to and read from another program. The <CODECLASS="literal">open</CODE> function lets you do one or the other, but not both.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1400">Solution</A></H3><PCLASS="para">Use the standard IPC::Open2 module:</P><PRECLASS="programlisting">use IPC::Open2;open2(*README, *WRITEME, $program);print WRITEME "here's your input\n";$output = <README>;close(WRITEME);close(README);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1420">Discussion</A></H3><PCLASS="para">Wanting simultaneous read and write access to another program is very common, but surprisingly perilous. That's why <CODECLASS="literal">open</CODE> doesn't let you say:</P><PRECLASS="programlisting">open(DOUBLE_HANDLE, "| program args |") # WRONG</PRE><PCLASS="para"><ACLASS="indexterm"NAME="ch16-idx-1000006320-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006320-1"></A>The big problem here is buffering. Because you can't force the other program to unbuffer its output, you can't guarantee that your reads won't block. If you block trying to read at the same time the other process blocks waiting for you to send something, you've achieved the unholy state of deadlock. There you'll both stay, wedged, until someone kills your process or the machine reboots.</P><PCLASS="para">If you control the other process's buffering because you wrote the other program and know how it works, then <ACLASS="indexterm"NAME="ch16-idx-1000006341-0"></A>IPC::Open2 may be the module for you. The first two arguments to <CODECLASS="literal">open2</CODE> that IPC::Open2 exports into your namespace are filehandles. Either pass references to typeglobs as in the Solution, or create your own IO::Handle objects and pass them in:</P><PRECLASS="programlisting">use IPC::Open2;use IO::Handle;($reader, $writer) = (IO::Handle->new, IO::Handle->new);open2($reader, $writer, $program);</PRE><PCLASS="para">If you pass in objects, you must have created them (with <CODECLASS="literal">IO::Handle->new</CODE>, for instance) first. The <CODECLASS="literal">open2</CODE> function will not create handles for you if you pass in variables that don't contain filehandles.</P><PCLASS="para">Alternatively, you can pass in arguments that look like <CODECLASS="literal">"<&OTHERFILEHANDLE"</CODE> or <CODECLASS="literal">">&OTHERFILEHANDLE"</CODE>, which specify existing filehandles for the child process to read from or write to. These filehandles don't have to be controlled by your program - they may be connected to other programs, files, or sockets.</P><PCLASS="para">You can specify the program either as a list (where the first element is the program name and the remaining elements are arguments to the program) or as a single string (which is passed to the shell as a command to start the program). If you also want control over the process's standard error, use the IPC::Open3 module and see the next recipe.</P><PCLASS="para">If an error occurs, <CODECLASS="literal">open2</CODE> and <CODECLASS="literal">open3</CODE> do not return. Instead, they <CODECLASS="literal">die</CODE> with an error message that begins with <CODECLASS="literal">"open2"</CODE> or <CODECLASS="literal">"open3"</CODE>. To test for failure, use the <CODECLASS="literal">eval</CODE> <CODECLASS="literal">BLOCK</CODE> construct:</P><PRECLASS="programlisting">eval { open2($readme, $writeme, @program_and_arguments);};if ($@) { if ($@ =~ /^open2/) { warn "open2 failed: $!\n$@\n"; return; } die; # reraise unforeseen exception}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1470">See Also</A></H3><PCLASS="para">The documentation for the IPC::Open2 and IPC::Open3 modules; <ACLASS="xref"HREF="ch10_13.htm"TITLE="Handling Exceptions">Recipe 10.12</A>; the <ACLASS="olink"HREF="../prog/ch03_032.htm"><CODECLASS="literal">eval</CODE></A> function in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <EMCLASS="emphasis">perlfunc </EM>(1); the <CODECLASS="literal">$@</CODE> variable in the section on <ACLASS="olink"HREF="../prog/ch02_09.htm#PERL2-CH-2-SECT-9.3">"Global Special Variables"</A> in <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <EMCLASS="emphasis">perlvar </EM>(1)</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_08.htm"TITLE="16.7. Reading STDERR from a Program"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.7. Reading STDERR from a Program"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_10.htm"TITLE="16.9. Controlling the Input, Output, and Error of Another Program"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.9. Controlling the Input, Output, and Error of Another Program"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">16.7. Reading STDERR from a Program</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">16.9. Controlling the Input, Output, and Error of Another Program</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -