📄 appa_13.htm
字号:
<html><head><title>Answers to Chapter 14 Exercises (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="appa_12.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="appa_14.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">A.13. Answers to Chapter 14 Exercises</h2><ol><li><p>Here's one way to do it:</p><blockquote><pre class="code">chdir "/" or die "Can't chdir to root directory: $!";exec "ls", "-l" or die "Can't exec ls: $!";</pre></blockquote><p>The first line changes the current working directory to the rootdirectory, as our particular hard-coded directory. The second lineuses the multiple-argument <tt class="literal">exec</tt> function to sendthe result to standard output. We could have used the single-argumentform just as well, but it doesn't hurt to do it this way.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">open STDOUT, ">ls.out" or die "Can't write to ls.out: $!";open STDERR, ">ls.err" or die "Can't write to ls.err: $!";chdir "/" or die "Can't chdir to root directory: $!";exec "ls", "-l" or die "Can't exec ls: $!";</pre></blockquote><p>The first and second lines reopen <tt class="literal">STDOUT</tt> and<tt class="literal">STDERR</tt> to a file in the current directory (beforewe change directories). Then, after the directory change, thedirectory listing command executes, sending the data back to thefiles opened in the original directory.</p><p>Where would the message from the last <tt class="literal">die</tt> go?Why, it would go into <em class="filename">ls.err</em>, of course, sincethat's where <tt class="literal">STDERR</tt> is going at that point.The <tt class="literal">die</tt> from <tt class="literal">chdir</tt> would gothere, too. But where would the message go if we can't re-open<tt class="literal">STDERR</tt> on the second line? It goes to the old<tt class="literal">STDERR</tt>. For the three standard filehandles,<tt class="literal">STDIN</tt>, <tt class="literal">STDOUT</tt>, and<tt class="literal">STDERR</tt>, if re-opening them fails, the oldfilehandle is still open.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">if (`date` =~ /^S/) { print "go play!\n";} else { print "get to work!\n";}</pre></blockquote><p>Well, since both Saturday and Sunday start with an S, and the day ofthe week is the first part of the output of the<i class="command">date</i> command, this is pretty simple. Just checkthe output of the <i class="command">date</i> command to see if it startswith <tt class="literal">S</tt>. There are many harder ways to do thisprogram, and we've seen most of them in our classes.</p><p>If we had to use this in a real-world program, though, we'dprobably use the pattern <tt class="literal">/^(Sat|Sun)/</tt>. It'sa tiny bit less efficient, but that hardly matters; besides,it's so much easier for the maintenance programmer tounderstand.</p></li></ol><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="appa_12.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="appa_14.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">A.12. Answers to Chapter 13 Exercises</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">A.14. Answers to Chapter 15 Exercises</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 + -