📄 appa_01.htm
字号:
<html><head><title>Exercise Answers (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="ch17_07.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div>
<h1 class="chapter">Appendix A. Exercise Answers</h1>
<div class="htmltoc"><h4 class="tochead">Contents:</h4>
<p> <a href="#lperl3-APP-A-SECT-1">Answers to Chapter 2 Exercises </a><br />
<a href="appa_02.htm">Answers to Chapter 3 Exercises</a><br />
<a href="appa_03.htm">Answers to Chapter 4 Exercises</a><br />
<a href="appa_04.htm">Answers to Chapter 5 Exercises</a><br />
<a href="appa_05.htm">Answers to Chapter 6 Exercises</a><br />
<a href="appa_06.htm">Answers to Chapter 7 Exercises</a><br />
<a href="appa_07.htm">Answers to Chapter 8 Exercises</a><br />
<a href="appa_08.htm">Answers to Chapter 9 Exercises</a><br />
<a href="appa_09.htm">Answer to Chapter 10 Exercise</a><br />
<a href="appa_10.htm">Answers to Chapter 11 Exercises</a><br />
<a href="appa_11.htm">Answers to Chapter 12 Exercises</a><br />
<a href="appa_12.htm">Answers to Chapter 13 Exercises</a><br />
<a href="appa_13.htm">Answers to Chapter 14 Exercises</a><br />
<a href="appa_14.htm">Answers to Chapter 15 Exercises</a><br />
<a href="appa_15.htm">Answers to Chapter 16 Exercises</a><br />
<a href="appa_16.htm">Answer to Chapter 17 Exercises</a><br /></p></div>
<p><a name="INDEX-1128" /></a>
<a name="INDEX-1129" /></a>This appendix contains the answers to the
excerses that appear throughout the book.
</p>
<div class="sect1"><a name="lperl3-APP-A-SECT-1" /></a>
<h2 class="sect1">A.1. Answers to Chapter 2 Exercises </h2>
<ol><li>
<p>Here's one way to do it:</p>
<blockquote><pre class="code">#!/usr/bin/perl -w
$pi = 3.141592654;
$circ = 2 * $pi * 12.5;
print "The circumference of a circle of radius 12.5 is $circ.\n";</pre></blockquote>
<p>As you see, we started this program with a typical
<tt class="literal">#!</tt> line; your path to Perl may vary. We also
turned on warnings.
</p>
<p>The first real line of code sets the value of <tt class="literal">$pi</tt>
to our value of <img align="absmiddle" src="figs/U03C0.gif" />. There are several reasons a good programmer
will prefer to use a constant<a href="#FOOTNOTE-379">[379]</a> value like
this: it takes time to type <tt class="literal">3.141592654</tt> into your
program if you ever need it more than once. It may be a mathematical
bug if you accidentally used <tt class="literal">3.141592654</tt> in one
place and <tt class="literal">3.14159</tt> in another. There's only
one line to check on to make sure you didn't accidentally type
<tt class="literal">3.141952654</tt> and send your space probe to the wrong
planet. It's easier to type <tt class="literal">$pi</tt> than <img align="absmiddle" src="figs/U03C0.gif" />,
especially if you don't have Unicode. And it will be easy to
maintain the program in case the value of <img align="absmiddle" src="figs/U03C0.gif" /> ever
changes.<a href="#FOOTNOTE-380">[380]</a>
</p><blockquote class="footnote"> <a name="FOOTNOTE-379" /></a><p>[379]If you'd prefer a
more formal sort of constants, the <tt class="literal">constant</tt> pragma
may be what you're looking for.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-380" /></a><p>[380]It nearly did change by a legislative act in
the state of Indiana. <a href="http://www.urbanlegends.com/legal/pi_indiana.html">http://www.urbanlegends.com/legal/pi_indiana.html</a></p>
</blockquote>
<p>Next we calculate the circumference, storing it into
<tt class="literal">$circ</tt>, and we print it out in a nice message. The
message ends with a newline character, because every line of a good
program's output should end with a newline. Without it, you
might end up with output looking something like this, depending upon
your shell's prompt:
</p>
<blockquote><pre class="code">The circumference of a circle of radius 12.5 is
78.53981635.bash-2.01$&cursor;</pre></blockquote>
<p>The box represents the input cursor, blinking at the end of the line,
and that's the shell's prompt at the end of the
message.<a href="#FOOTNOTE-381">[381]</a> Since the
circumference isn't really
<tt class="literal">78.53981635.bash-2.01$</tt>, this should probably be
construed as a bug. So use <tt class="literal">\n</tt> at the end of each
line of output.
</p><blockquote class="footnote"> <a name="FOOTNOTE-381" /></a><p>[381]We asked O'Reilly to spend the extra
money to print the input cursor with blinking ink, but they
wouldn't do it for us.</p> </blockquote>
</li>
<li>
<p>Here's one way to do it:</p>
<blockquote><pre class="code">#!/usr/bin/perl -w
$pi = 3.141592654;
print "What is the radius? ";
chomp($radius = <STDIN>);
$circ = 2 * $pi * $radius;
print "The circumference of a circle of radius $radius is $circ.\n";</pre></blockquote>
<p>This is just like the last one, except that now we ask the user for
the radius, and then we use <tt class="literal">$radius</tt> in every place
where we previously used the hard-coded value
<tt class="literal">12.5</tt>. If we had written the first program with
more foresight, in fact, we would have had a variable named
<tt class="literal">$radius</tt> in that one as well. Note that we
<tt class="literal">chomp</tt>ed the line of input. If we hadn't, the
mathematical formula would still have worked, because a string like
<tt class="literal">"12.5\n"</tt> is converted to the number
<tt class="literal">12.5</tt> without any problem. But when we print out
the message, it would look like this:
</p>
<blockquote><pre class="code">The circumference of a circle of radius 12.5
is 78.53981635.</pre></blockquote>
<p>Notice that the newline character is still in
<tt class="literal">$radius</tt>, even though we've used that
variable as a number. Since we had a space between
<tt class="literal">$radius</tt> and the word
"<tt class="literal">is</tt>" in the <tt class="literal">print</tt>
statement, there's a space at the beginning of the second line
of output. The moral of the story is: <tt class="literal">chomp</tt> your
input unless you have a reason not to do that.
</p>
</li>
<li>
<p>Here's one way to do it:</p>
<blockquote><pre class="code">#!/usr/bin/perl -w
$pi = 3.141592654;
print "What is the radius? ";
chomp($radius = <STDIN>);
$circ = 2 * $pi * $radius;
if ($radius < 0) {
$circ = 0;
}
print "The circumference of a circle of radius $radius is $circ.\n";</pre></blockquote>
<p>Here we added the check for a bogus radius. Even if the given radius
was impossible, the returned circumference will at least be
nonnegative. You could have changed the given radius to be zero, and
then calculated the circumference, too; there's more than one
way to do it. In fact, that's the Perl motto: There Is More
Than One Way To Do It. And that's why each exercise answer
starts with "Here's one way to do it."
</p>
</li>
<li>
<p>Here's one way to do it:</p>
<blockquote><pre class="code">print "Enter first number: ";
chomp($one = <STDIN>);
print "Enter second number: ";
chomp($two = <STDIN>);
$result = $one * $two;
print "The result is $result.\n";</pre></blockquote>
<p>Notice that we've left off the <tt class="literal">#!</tt> line for
this answer. In fact, from here on, we'll assume that you know
it's there, so you don't need to read it each time.
</p>
<p>Perhaps those are poor choices for variable names. In a large
program, a maintenance programmer might think that
<tt class="literal">$two</tt> should have the value of
<tt class="literal">2</tt>. In this short program, it probably
doesn't matter, but in a large one we could have called them
something more descriptive, with names like
<tt class="literal">$first_response</tt>.
</p>
<p>In this program, it wouldn't make any difference if we forgot
to <tt class="literal">chomp</tt> the two variables <tt class="literal">$one</tt>
and <tt class="literal">$two</tt>, since we never use them as strings once
they've been set. But if next week our maintenance programmer
edits the program to print a message like: <tt class="literal">The result of
multiplying $one by $two is $result.\n</tt>, those pesky
newlines will come back to haunt us. Once again,
<tt class="literal">chomp</tt> unless you have a reason not to
<tt class="literal">chomp<a href="#FOOTNOTE-382">[382]</a></tt> -- like in the next exercise.
</p><blockquote class="footnote"> <a name="FOOTNOTE-382" /></a><p>[382]Chomping is like chewing -- not
always needed, but most of the time it doesn't hurt.</p>
</blockquote>
</li>
<li>
<p>Here's one way to do it:</p>
<blockquote><pre class="code">print "Enter a string: ";
$str = <STDIN>;
print "Enter a number of times: ";
chomp($num = <STDIN>);
$result = $str x $num;
print "The result is:\n$result";</pre></blockquote>
<p>This program is almost the same as the last one, in a sense.
We're "multiplying" a string by a number of times.
So we've kept the structure of the previous exercise. In this
case, though, we didn't want to <tt class="literal">chomp</tt> the
first input item -- the string -- because the exercise asked
for the strings to appear on separate lines. So, if the user entered
<tt class="literal">fred</tt> and a newline for the string, and
<tt class="literal">3</tt> for the number, we'd get a newline after
each <tt class="literal">fred</tt> just as we wanted.
</p>
<p>In the <tt class="literal">print</tt> statement at the end, we put the
newline before <tt class="literal">$result</tt> because we wanted to have
the first <tt class="literal">fred,</tt> printed on a line of its own. That
is, we didn't want output like this, with only two of the three
<tt class="literal">fred</tt>s aligned in a column:
</p>
<blockquote><pre class="code">The result is: fred
fred
fred</pre></blockquote>
<p>At the same time, we didn't need to put another newline at the
end of the <tt class="literal">print</tt> output because
<tt class="literal">$result</tt> should already end with a newline.
</p>
<p>In most cases, Perl won't mind where you put spaces in your
program; you can put in spaces or leave them out. But it's
important not to accidentally spell the wrong thing! If the
<tt class="literal">x</tt> runs up against the preceding variable name
<tt class="literal">$str</tt>, Perl will see <tt class="literal">$strx</tt>,
which won't work.
</p>
</li></ol>
</div>
<hr width="684" align="left" />
<div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch17_07.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">17.7. Exercise</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.2. Answers to Chapter 3 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 + -