📄 ch14_02.htm
字号:
<html><head><title>The exec Function (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#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="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch14_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch14_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">14.2. The exec Function</h2><p>Everything we've just said about <tt class="literal">system</tt>syntax and semantics is also true about the<tt class="literal">exec</tt><a name="INDEX-959" /> function, except for one (veryimportant) thing. The <tt class="literal">system</tt> function creates achild process, which then scurries off to perform the requestedaction while Perl naps. The <tt class="literal">exec</tt> function causesthe Perl process <em class="emphasis">itself</em> to perform the requestedaction. Think of it as more like a "goto" than asubroutine call.</p><p>For example, suppose we wanted to run the <i class="command">bedrock</i>command in the <em class="filename">/tmp</em> directory, passing itarguments of <em class="emphasis">-o args1</em> followed by whateverarguments our own program was invoked with. That'd look likethis:</p><blockquote><pre class="code">chdir "/tmp" or die "Cannot chdir /tmp: $!";exec "bedrock", "-o", "args1", @ARGV;</pre></blockquote><p>When we reach the <tt class="literal">exec</tt> operation, Perl locates<i class="command">bedrock</i>, and "jumps into it." At thatpoint, there is no Perl process any more,<a href="#FOOTNOTE-314">[314]</a> just the process running the<i class="command">bedrock</i> command. When <i class="command">bedrock</i>is finished, there's no Perl to come back to, so we'd geta prompt back if we invoked this program from the command line.</p><blockquote class="footnote"> <a name="FOOTNOTE-314" /><p>[314]Actually,it's the same process, having performed the Unix<tt class="literal">exec(2)</tt> system call (or equivalent). The processID remains the same.</p> </blockquote><p>Why is this useful? Well, if the purpose of this Perl program were toset up a particular environment to run another program, the purposeis fulfilled as soon as the other program has started. If we'dused <tt class="literal">system</tt> instead of<tt class="literal">exec</tt>, we'd have a Perl program juststanding around tapping its toes waiting for the other program tocomplete, just so Perl could finally immediately exit as well, andthat's a wasted resource.</p><p>Having said that, it's actually quite rare to use<tt class="literal">exec</tt>, except in combination with<tt class="literal">fork</tt> (which we'll see later). If you arepuzzling over <tt class="literal">system</tt><a name="INDEX-960" /> versus <tt class="literal">exec</tt>,just pick <tt class="literal">system</tt>, and nearly all of the time,you'll be just fine.</p><p>Because Perl is no longer in control once the requested command hasstarted, it doesn't make any sense to have any Perl codefollowing the <tt class="literal">exec</tt>, except for handling theerror when the requested command cannot be started:</p><blockquote><pre class="code">exec "date";die "date couldn't run: $!";</pre></blockquote><p>In fact, if you have warnings turned on, and if you have any codeafter the <tt class="literal">exec</tt> other than a<tt class="literal">die</tt>,<a href="#FOOTNOTE-315">[315]</a> you'll get notified.</p><blockquote class="footnote"> <a name="FOOTNOTE-315" /><p>[315]Or exit. Or if it'sat the end of a block. This may change in a new release of Perl,too.</p> </blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch14_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch14_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">14. Process Management</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">14.3. The Environment Variables</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><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 + -