appa_15.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 121 行
HTM
121 行
<html><head><title>Answers to Chapter 16 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_14.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_16.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">A.15. Answers to Chapter 16 Exercises</h2><ol><li><p>Here's one way to do it:</p><blockquote><pre class="code">open PF, '/path/to/perlfunc.pod' or die "Can't open perlfunc.pod: $!";dbmopen my %DB, "pf_data", 0644 or die "Can't create dbm file: $!";%DB = ( ); # wipe existing data, if anywhile (<PF>) { if (/^=item\s+([a-z_]\w*)/i) { $DB{$1} = $DB{$1} || $.; }}print "Done!\n";</pre></blockquote><p>This one is similar to the previous ones with<em class="filename">perlfunc.pod</em>. Here, though, we open a DBM filecalled <em class="filename">pf_data</em> as the DBM hash<tt class="literal">%DB</tt>. In case that file had any leftover data, weset the hash to an empty list. That's normally a rare thing todo, but we want to wipe out the entire database, in case a previousrun of this program left incorrect or out-of-date data in the file.(After all, there's a new <em class="filename">perlfunc.pod</em>with each new release of Perl.)</p><p>When we find an identifier, we need to store its line number (from<tt class="literal">$.</tt>) into the database. The statement that doesthat uses the high-precedence short-circuit <tt class="literal">||</tt>operator. If the database entry already has a value, that value istrue, so the old value is used. If the database entry is empty,that's false, so the value on the right (<tt class="literal">$.</tt>)is used instead. We could have written that line in a shorter way,like this:</p><blockquote><pre class="code">$DB{$1} ||= $.;</pre></blockquote><p>When the program is done, it says so. That's not required bythe exercise description, but it lets us know that the program didsomething; without that line, there would be no output at all. Buthow can we tell that it worked correctly? That's the nextexercise.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">dbmopen my %DB, "pf_data", undef or die "Can't open dbm file: $!";my $line = $DB{$ARGV[0]} || "not found";print "$ARGV[0]: $line\n";</pre></blockquote><p>Once we have the database, it's simple to look something up init. Note that in this program, the third argument to<tt class="literal">dbmopen</tt> is <tt class="literal">undef</tt>, since thatfile must already exist for this program to work.</p><p>If the entry for <tt class="literal">$ARGV[0]</tt> (the first command-lineparameter) isn't found in the database, we'll sayit's <tt class="literal">not found</tt>, using the high-precedenceshort-circuit <tt class="literal">||</tt>.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">dbmopen my %DB, "pf_data", undef or die "Can't open dbm file: $!";if (my $line = $DB{$ARGV[0]}) { exec 'less', "+$line", '/path/to/perlfunc.pod' or die "Can't exec pager: $!";} else { die "Entry unknown: '$ARGV[0]'.\n";}</pre></blockquote><p>This starts out like the previous one, but uses<tt class="literal">exec</tt> to start up a pager program if it can, anddies if it can't.</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_14.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_16.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">A.14. Answers to Chapter 15 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.16. Answer to Chapter 17 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 + =
减小字号Ctrl + -
显示快捷键?