欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

ch03_14.htm

编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
HTM
字号:
<html><head><title>C-Style Logical (Short-Circuit) Operators (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="C-Style Logical (Short-Circuit) Operators"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch03_13.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch03_01.htm">Chapter 3: Unary and Binary Operators</a></td><td align="right" valign="top" width="172"><a href="ch03_15.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">3.14. C-Style Logical (Short-Circuit) Operators</h2><p><a name="INDEX-964"></a><a name="INDEX-965"></a><a name="INDEX-966"></a><a name="INDEX-967"></a><a name="INDEX-968"></a><a name="INDEX-969"></a><a name="INDEX-970"></a><a name="INDEX-971"></a><a name="INDEX-972"></a>Like C, Perl provides the <tt class="literal">&amp;&amp;</tt> (logical AND) and <tt class="literal">||</tt> (logicalOR) operators.  They evaluate from left to right (with <tt class="literal">&amp;&amp;</tt>having slightly higher precedence than <tt class="literal">||</tt>) testing the truth of thestatement.  These operators are known as short-circuit operators becausethey determine the truth of the statement by evaluating the fewestnumber of operands possible.  For example, if the left operand of an<tt class="literal">&amp;&amp;</tt> operator is false, the right operand is never evaluatedbecause the result of the operator is false regardless of the value ofthe right operand.</p><a name="perl3-tab-logical"></a><table border="1"><tr><th>Example</th><th>Name</th><th>Result</th></tr><tr><td><tt class="literal">$a &amp;&amp; $b</tt></td><td>And</td><td><tt class="literal">$a</tt> if <tt class="literal">$a</tt> is false, <tt class="literal">$b</tt> otherwise</td></tr><tr><td><tt class="literal">$a || $b</tt></td><td>Or</td><td><p><tt class="literal">$a</tt> if <tt class="literal">$a</tt> is true, <tt class="literal">$b</tt> otherwise</p></td></tr></table><p>Such short circuits not only save time, but are frequently used tocontrol the flow of evaluation.  For example, an oft-appearing idiom inPerl programs is:<blockquote><pre class="programlisting">open(FILE, "somefile") || die "Can't open somefile: $!\n";</pre></blockquote>In this case, Perl first evaluates the<tt class="literal">open</tt> function.  If the value is true (because<em class="emphasis">somefile</em> was successfully opened), the executionof the <tt class="literal">die</tt> function is unnecessary, and so isskipped.  You can read this literally as "Open some file or die!"</p><p>The <tt class="literal">&amp;&amp;</tt> and <tt class="literal">||</tt> operatorsdiffer from C's in that, rather than returning 0 or 1, they return thelast value evaluated.  In the case of <tt class="literal">||</tt>, this hasthe delightful result that you can select the first of a series ofscalar values that happens to be true.  Thus, a reasonably portableway to find out the user's home directory might be:<blockquote><pre class="programlisting">$home = $ENV{HOME}      || $ENV{LOGDIR}      || (getpwuid($&lt;))[7]      || die "You're homeless!\n";</pre></blockquote>On the other hand, since the left argument is always evaluated in scalarcontext, you can't use <tt class="literal">||</tt> for selecting between two aggregatesfor assignment:<blockquote><pre class="programlisting">@a = @b || @c;             # This doesn't do the right thing@a = scalar(@b) || @c;     # because it really means this.@a = @b ? @b : @c;         # This works fine, though.</pre></blockquote>Perl also provides lower precedence <tt class="literal">and</tt> and<tt class="literal">or</tt> operators that some people find more readableand don't force you to use parentheses on list operators.  They alsoshort-circuit. See <a href="ch01_05.htm#perl3-tab-over-logical">Table 3-1</a> for acomplete list.<a name="INDEX-973"></a></p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch03_13.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch03_15.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">3.13. Bitwise Operators</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">3.15. Range Operator</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -