📄 ch16_02.htm
字号:
<HTML><HEAD><TITLE>Recipe 16.1. Gathering Output from a 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:37Z"><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_01.htm"TITLE="16.0. Introduction"><LINKREL="next"HREF="ch16_03.htm"TITLE="16.2. Running 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_01.htm"TITLE="16.0. Introduction"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.0. Introduction"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_03.htm"TITLE="16.2. Running Another Program"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.2. Running Another Program"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch16-17269">16.1. Gathering Output from a Program</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-760">Problem<ACLASS="indexterm"NAME="ch16-idx-1000006238-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006238-1"></A><ACLASS="indexterm"NAME="ch16-idx-1000006238-2"></A></A></H3><PCLASS="para">You want to run a program and collect its output into a variable.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-766">Solution</A></H3><PCLASS="para">Either use backticks: <ACLASS="indexterm"NAME="ch16-idx-1000006244-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006244-1"></A></P><PRECLASS="programlisting">$output = `program args`; # collect output into one multiline string@output = `program args`; # collect output into array, one line per element</PRE><PCLASS="para">Or use <ACLASS="xref"HREF="ch16_05.htm"TITLE="Reading or Writing to Another Program">Recipe 16.4</A>:</P><PRECLASS="programlisting">open(README, "program args |") or die "Can't run program: $!\n";while(<README>) { $output .= $_;}close(README);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-788">Discussion</A></H3><PCLASS="para">The backticks are a convenient way to run other programs and gather their output. The backticks do not return until the called program exits. Perl goes to some trouble behind the scenes to collect the output, so it is inefficient to use the backticks and ignore their return value:</P><PRECLASS="programlisting">`fsck -y /dev/rsd1a`; # BAD AND SCARY</PRE><PCLASS="para">Both the <CODECLASS="literal">open</CODE> function and the backtick operator call the shell to run their commands. This makes them unsafe when used in a program with special privileges.</P><PCLASS="para">A high-level workaround is given in <ACLASS="xref"HREF="ch19_07.htm"TITLE="Executing Commands Without Shell Escapes">Recipe 19.6</A>. Here's a low-level workaround, using <CODECLASS="literal">pipe</CODE>, <CODECLASS="literal">fork</CODE>, and <CODECLASS="literal">exec</CODE>:</P><PRECLASS="programlisting">use POSIX qw(:sys_wait_h);pipe(README, WRITEME);if ($pid = fork) { # parent $SIG{CHLD} = sub { 1 while ( waitpid(-1, WNOHANG)) > 0 }; close(WRITEME);} else { die "cannot fork: $!" unless defined $pid; # child open(STDOUT, ">&=WRITEME") or die "Couldn't redirect STDOUT: $!"; close(README); exec($program, $arg1, $arg2) or die "Couldn't run $program : $!\n";}while (<README>) { $string .= $_; # or push(@strings, $_);}close(README);<ACLASS="indexterm"NAME="ch16-idx-1000006247-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006247-1"></A><ACLASS="indexterm"NAME="ch16-idx-1000006247-2"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000006053">See Also</A></H3><PCLASS="para">The section on <ACLASS="olink"HREF="../prog/ch06_03.htm">"Cooperating with Strangers"</A> in <ACLASS="olink"HREF="../prog/ch06_01.htm">Chapter 6</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>, or <ICLASS="filename">perlsec </I>(1); <ACLASS="xref"HREF="ch16_03.htm"TITLE="Running Another Program">Recipe 16.2</A>; <ACLASS="xref"HREF="ch16_05.htm"TITLE="Reading or Writing to Another Program">Recipe 16.4</A>; <ACLASS="xref"HREF="ch16_11.htm"TITLE="Communicating Between Related Processes">Recipe 16.10</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_01.htm"TITLE="16.0. Introduction"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.0. Introduction"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_03.htm"TITLE="16.2. Running Another Program"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.2. Running Another Program"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">16.0. Introduction</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.2. Running 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 + -