⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch10_09.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<p>Here's a trick you might see, used to code up a nice multiwaybranch:</p><blockquote><pre class="code">my $size =  ($width &lt; 10) ? "small"  :  ($width &lt; 20) ? "medium" :  ($width &lt; 50) ? "large"  :                  "extra-large"; # default</pre></blockquote><p>That is really just three nested <tt class="literal">?:</tt> operators, andit works quite well, once you get the hang of it.</p><p>Of course, you're not obliged to use this operator. Beginnersmay wish to avoid it. But you'll see it in others' code,sooner or later, and we hope that one day you'll find a goodreason to use it in your own programs.</p></div><a name="lperl3-CHP-10-SECT-9.3" /><div class="sect2"><h3 class="sect2">10.9.3. Control Structures Using Partial-Evaluation Operators</h3><p><a name="INDEX-719" /> <a name="INDEX-720" />These three operators that we'vejust seen -- <tt class="literal">&amp;&amp;</tt>, <tt class="literal">||</tt>,and <tt class="literal">?:</tt> -- all share a peculiar property:depending upon whether the value on the left side is true or false,they may or may not evaluate an expression. Sometimes the expressionis evaluated, and sometimes it isn't. For that reason, theseare sometimes called <em class="firstterm">partial-evaluation</em>operators, since they may not evaluate all of the expressions aroundthem. And partial-evaluation operators are automatically controlstructures.<a href="#FOOTNOTE-238">[238]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-238" /><p>[238]Some of you were wondering why theselogical operators are being covered in this chapter, weren'tyou?</p> </blockquote><p>It's not as if Larry felt a burning need to add more controlstructures to Perl. But once he had decided to put thesepartial-evaluation operators into Perl, they automatically becamecontrol structures as well. After all, anything that can activate anddeactivate a chunk of code is a control structure.</p><p>Fortunately, you'll notice this only when the controlledexpression has side effects, like altering a variable's valueor causing some output. For example, suppose you ran across this lineof code:</p><blockquote><pre class="code">($a &lt; $b) &amp;&amp; ($a = $b);</pre></blockquote><p>Right away, you should notice that the result of the logical ANDisn't being assigned anywhere.<a href="#FOOTNOTE-239">[239]</a> Why not?</p><blockquote class="footnote"> <a name="FOOTNOTE-239" /><p>[239]But don'tforget to consider that it might be a return value, as the lastexpression in a subroutine.</p> </blockquote><p>If <tt class="literal">$a</tt> is really less than <tt class="literal">$b</tt>,the left side is true, so the right side will be evaluated, therebydoing the assignment. But if <tt class="literal">$a</tt> is not less than<tt class="literal">$b</tt>, the left side will be false, and thus theright side would be skipped. So that line of code would doessentially the same thing as this one, which is easier tounderstand:</p><blockquote><pre class="code">if ($a &lt; $b) { $a = $b; }</pre></blockquote><p>Or maybe you'll be maintaining a<a name="INDEX-721" />program, and you'll see a line likethis one:</p><blockquote><pre class="code">($a &gt; 10) || print "why is it not greater?\n";</pre></blockquote><p>If <tt class="literal">$a</tt> is really greater than ten, the left side istrue, and the logical OR is done. But if it's not, the leftside is false, and this will go on to print the message. Once again,this could (and probably should) be written in the traditional way,probably with <tt class="literal">if</tt> or <tt class="literal">unless</tt>.</p><p>If you have a particularly twisted brain, you might even learn toread these lines as if they were written in English. For example:check that <tt class="literal">$a</tt> is less than <tt class="literal">$b</tt>,<em class="emphasis">and if it is</em>, then do the assignment. Check that<tt class="literal">$a</tt> is more than ten, <em class="emphasis">or if it'snot</em>, then print the message.</p><p>It's generally former C programmers or old-time Perlprogrammers who most often use these ways of writing controlstructures. Why do they do it? Some have the mistaken idea that theseare more efficient. Some think these tricks make their code cooler.Some are merely copying what they saw someone else do.</p><p>In the same way, the ternary operator may be used for control. Inthis case, we want to assign <tt class="literal">$c</tt> to the smaller oftwo variables:</p><blockquote><pre class="code">($a &lt; $b) ? ($a = $c) : ($b = $c);</pre></blockquote><p>If <tt class="literal">$a</tt> is smaller, it gets <tt class="literal">$c</tt>.Otherwise, <tt class="literal">$b</tt> does.</p><p>There is another way to write the logical<a name="INDEX-722" /><a name="INDEX-723" />AND and logical<a name="INDEX-724" />ORoperators. You may wish to write them out as words:<tt class="literal">and</tt> and <tt class="literal">or</tt>.<a href="#FOOTNOTE-240">[240]</a> Theseword-operators have the same behaviors as the ones written withpunctuation, but the words are much lower on the precedence chart.Since the words don't "stick" so tightly to thenearby parts of the expression, they may need fewer parentheses:</p><blockquote class="footnote"><a name="FOOTNOTE-240" /><p>[240]There are also the low-precedence<a name="INDEX-725" /> <a name="INDEX-726" /> <tt class="literal">not</tt> (like thelogical-negation operator, "<tt class="literal">!</tt>") andthe rare <tt class="literal">xor</tt>.</p> </blockquote><blockquote><pre class="code">$a &lt; $b and $a = $b;  # but better written as the corresponding if</pre></blockquote><p>Then again, you may need <em class="emphasis">more</em> parentheses.<a name="INDEX-727" />Precedence is a bugaboo. Be sureto use parentheses to say what you mean, unless you're sure ofthe precedence. Nevertheless, since the word forms are very lowprecedence, you can generally understand that they cut the expressioninto big pieces, doing everything on the left first, and then (ifneeded) everything on the right.</p><p>Despite the fact that using logical operators as control structurescan be confusing, sometimes they're the accepted way to writecode. We'll see a common use of the <tt class="literal">or</tt>operator starting in the next chapter.</p><p>So, using these operators as control structures is part of idiomaticPerl -- Perl as she is spoken. Used properly, they can make yourcode more powerful; otherwise they can make your code unmaintainable.Don't overuse<a name="INDEX-728" /> <a name="INDEX-729" /> them.<a href="#FOOTNOTE-241">[241]</a><a name="INDEX-730" /> <a name="INDEX-731" /> </p><blockquote class="footnote"><a name="FOOTNOTE-241" /><p>[241]Using these weird forms more than once per month counts asoveruse.</p> </blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch10_08.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="ch10_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">10.8. Loop Controls</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">10.10. Exercise</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 &copy; 2002</a> O'Reilly &amp; 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 + -