📄 ch16_05.htm
字号:
<HTML><HEAD><TITLE>Recipe 16.4. Reading or Writing to 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:41Z"><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_04.htm"TITLE="16.3. Replacing the Current Program with a Different One"><LINKREL="next"HREF="ch16_06.htm"TITLE="16.5. Filtering Your Own Output"></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_04.htm"TITLE="16.3. Replacing the Current Program with a Different One"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.3. Replacing the Current Program with a Different One"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_06.htm"TITLE="16.5. Filtering Your Own Output"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.5. Filtering Your Own Output"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch16-16252">16.4. Reading or Writing to Another Program</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-988">Problem<ACLASS="indexterm"NAME="ch16-idx-1000006259-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006259-1"></A><ACLASS="indexterm"NAME="ch16-idx-1000006259-2"></A><ACLASS="indexterm"NAME="ch16-idx-1000006259-3"></A><ACLASS="indexterm"NAME="ch16-idx-1000006259-4"></A></A></H3><PCLASS="para">You want to run another program and either read its output or supply the program with input.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-994">Solution</A></H3><PCLASS="para">Use <CODECLASS="literal">open</CODE><ACLASS="indexterm"NAME="ch16-idx-1000006265-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006265-1"></A> with a pipe symbol at the beginning or end. To read from a program, put the pipe symbol at the end:</P><PRECLASS="programlisting">$pid = open(README, "program arguments |") or die "Couldn't fork: $!\n";while (<README>) { # ...}close(README) or die "Couldn't close: $!\n";</PRE><PCLASS="para">To write to the program, put the pipe at the beginning:</P><PRECLASS="programlisting">$pid = open(WRITEME, "| program arguments") or die "Couldn't fork: $!\n";print WRITEME "data\n";close(WRITEME) or die "Couldn't close: $!\n";</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1018">Discussion</A></H3><PCLASS="para">In the case of reading, this is similar to using backticks, except you have a process ID and a filehandle. As with the backticks, <CODECLASS="literal">open</CODE> uses the shell if it sees shell-special characters in its argument, but it doesn't if there aren't any. This is usually a welcome convenience, because it lets the shell do filename wildcard expansion and I/O redirection, saving you the trouble.</P><PCLASS="para">However, sometimes this isn't desirable. Piped <CODECLASS="literal">open</CODE>s that include unchecked user data would be unsafe while running in taint mode or in untrustworthy situations. <ACLASS="xref"HREF="ch19_07.htm"TITLE="Executing Commands Without Shell Escapes">Recipe 19.6</A> shows how to get the effect of a piped <CODECLASS="literal">open</CODE> without risking using the shell.</P><PCLASS="para">Notice how we specifically call <CODECLASS="literal">close</CODE> on the filehandle. When you use <CODECLASS="literal">open</CODE> to connect a filehandle to a child process, Perl remembers this and automatically waits for the child when you close the filehandle. If the child hasn't exited by then, Perl waits until it does. This can be a very, very long wait if your child doesn't exit:</P><PRECLASS="programlisting">$pid = open(F, "sleep 100000|"); # child goes to sleepclose(F); # and the parent goes to lala land</PRE><PCLASS="para">To avoid this, you can save the PID returned by <CODECLASS="literal">open</CODE> to kill your child, or use a manual <CODECLASS="literal">pipe</CODE>-<CODECLASS="literal">fork</CODE>-<CODECLASS="literal">exec</CODE> sequence as described in <ACLASS="xref"HREF="ch16_11.htm"TITLE="Communicating Between Related Processes">Recipe 16.10</A>.</P><PCLASS="para">If you attempt to write to a process that has gone away, your process will receive a <ACLASS="indexterm"NAME="ch16-idx-1000006280-0"></A>SIGPIPE. The default disposition for this signal is to kill your process, so the truly paranoid install a SIGPIPE handler just in case.</P><PCLASS="para">If you want to run another program and be able to supply its STDIN yourself, a similar construct is used:</P><PRECLASS="programlisting">$pid = open(WRITEME, "| program args");print WRITEME "hello\n"; # program will get hello\n on STDINclose(WRITEME); # program will get EOF on STDIN</PRE><PCLASS="para">The leading pipe symbol in the filename argument to <CODECLASS="literal">open</CODE> tells Perl to start another process instead. It connects the <CODECLASS="literal">open</CODE>ed filehandle to the process's STDIN. Anything you write to the filehandle can be read by the program through its STDIN. When you <CODECLASS="literal">close</CODE> the filehandle, the <CODECLASS="literal">open</CODE>ed process will get an <CODECLASS="literal">eof</CODE> when it next tries to read from STDIN.</P><PCLASS="para">You can also use this technique to change your program's normal output path. For example, to automatically run everything through a pager, use something like:</P><PRECLASS="programlisting">$pager = $ENV{PAGER} || '/usr/bin/less'; # XXX: might not existopen(STDOUT, "| $pager");</PRE><PCLASS="para">Now, without changing the rest of your program, anything you print to standard output will go through the pager automatically.</P><PCLASS="para">As with <CODECLASS="literal">open</CODE>ing a process for reading, text passed to the shell here is subject to shell metacharacter interpretation. To avoid the shell, a similar solution is called for. As before, the parent should also be wary of <CODECLASS="literal">close</CODE>. If the parent closes the filehandle connecting it to the child, the parent will block while waiting for the child to finish. If the child doesn't finish, neither will the close. The workaround as before is either to kill your child process prematurely, or else use the low-level <CODECLASS="literal">pipe</CODE>-<CODECLASS="literal">fork</CODE>-<CODECLASS="literal">exec</CODE> scenario.</P><PCLASS="para">When using piped opens, always check return values of both <CODECLASS="literal">open</CODE> and <CODECLASS="literal">close</CODE>, not just of <CODECLASS="literal">open</CODE>. That's because the return value from <CODECLASS="literal">open</CODE> does not indicate whether the command was succesfully launched. With a piped open, you fork a child to execute the command. Assuming the system hadn't run out of processes, the <CODECLASS="literal">fork</CODE> immediately returns the PID of the child it just created.<CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch16-idx-1000006267-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006267-1"></A></P><PCLASS="para">By the time the child process tries to <CODECLASS="literal">exec</CODE> the command, it's a separately scheduled process. So if the command can't be found, there's effectively no way to communicate this back to the <CODECLASS="literal">open</CODE> function, because that function is in a different process!</P><PCLASS="para">Check the return value from <CODECLASS="literal">close</CODE> to see whether the command was successful. If the child process exits with non-zero status - which it will do if the command isn't found - the <CODECLASS="literal">close</CODE> returns false and <CODECLASS="literal">$?</CODE> is set to the wait status of that process. You can interpret its contents as described in <ACLASS="xref"HREF="ch16_20.htm"TITLE="Avoiding Zombie Processes">Recipe 16.19</A>.</P><PCLASS="para">In the case of a pipe opened for writing, you should also install a SIGPIPE handler, since writing to a child that isn't there will trigger a SIGPIPE.<ACLASS="indexterm"NAME="ch16-idx-1000006261-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006261-1"></A><ACLASS="indexterm"NAME="ch16-idx-1000006261-2"></A><ACLASS="indexterm"NAME="ch16-idx-1000006261-3"></A><ACLASS="indexterm"NAME="ch16-idx-1000006261-4"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1064">See Also</A></H3><PCLASS="para">The <ACLASS="olink"HREF="../prog/ch03_102.htm"><CODECLASS="literal">open</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 <ICLASS="filename">perlfunc </I>(1); <ACLASS="xref"HREF="ch16_11.htm"TITLE="Communicating Between Related Processes">Recipe 16.10</A>; <ACLASS="xref"HREF="ch16_16.htm"TITLE="Installing a Signal Handler">Recipe 16.15</A>; <ACLASS="xref"HREF="ch16_20.htm"TITLE="Avoiding Zombie Processes">Recipe 16.19</A>; <ACLASS="xref"HREF="ch19_07.htm"TITLE="Executing Commands Without Shell Escapes">Recipe 19.6</A></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_04.htm"TITLE="16.3. Replacing the Current Program with a Different One"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.3. Replacing the Current Program with a Different One"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_06.htm"TITLE="16.5. Filtering Your Own Output"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.5. Filtering Your Own Output"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">16.3. Replacing the Current Program with a Different One</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.5. Filtering Your Own Output</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 + -