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

📄 ch03_20.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Logical and, or, not, and xor (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="Logical and, or, not, and xor"><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_19.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_21.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.20. Logical and, or, not, and xor</h2><p><a name="INDEX-1017"></a><a name="INDEX-1018"></a><a name="INDEX-1019"></a><a name="INDEX-1020"></a><a name="INDEX-1021"></a><a name="INDEX-1022"></a><a name="INDEX-1023"></a>As lower precedence alternatives to <tt class="literal">&amp;&amp;</tt>, <tt class="literal">||</tt>, and <tt class="literal">!</tt>, Perlprovides the <tt class="literal">and</tt>, <tt class="literal">or</tt>, and <tt class="literal">not</tt> operators.  The behavior of theseoperators is identical--in particular, <tt class="literal">and</tt> and <tt class="literal">or</tt> short-circuit liketheir counterparts, which makes them useful not only for logicalexpressions but also for control flow.</p><p>Since the precedence of these operators is much lower than the onesborrowed from C, you can safely use them after a list operator withoutthe need for parentheses:<blockquote><pre class="programlisting">unlink "alpha", "beta", "gamma"        or gripe(), next LINE;</pre></blockquote>With the C-style operators you'd have to write it like this:<blockquote><pre class="programlisting">unlink("alpha", "beta", "gamma")        || (gripe(), next LINE);</pre></blockquote>But you can't just up and replace all instances of<tt class="literal">||</tt> with <tt class="literal">or</tt>.  Suppose you changethis:<blockquote><pre class="programlisting">$xyz = $x || $y || $z;</pre></blockquote>to this:<blockquote><pre class="programlisting">$xyz = $x or $y or $z;    # WRONG</pre></blockquote>That wouldn't do the same thing at all!  The precedence of theassignment is higher than <tt class="literal">or</tt> but lower than<tt class="literal">||</tt>, so it would always assign <tt class="literal">$x</tt>to <tt class="literal">$xyz</tt>, and then do the <tt class="literal">or</tt>s.To get the same effect as <tt class="literal">||</tt>, you'd have to write:<blockquote><pre class="programlisting">$xyz = ( $x or $y or $z );</pre></blockquote></p><p>The moral of the story is that you still must learn precedence (or useparentheses) no matter which variety of logical operators you use.</p><p>There is also a logical <tt class="literal">xor</tt> operator that has noexact counterpart in C or Perl, since the only other exclusive-ORoperator (<tt class="literal">(^)</tt>) works on bits.  The<tt class="literal">xor</tt> operator can't short-circuit, since both sidesmust be evaluated.  The best equivalent for <tt class="literal">$a xor$b</tt> is perhaps <tt class="literal">!$a != !$b</tt>. One could alsowrite <tt class="literal">!$a ^ !$b</tt> or even <tt class="literal">$a ? !$b :!!$b</tt>, of course.  The point is that both<tt class="literal">$a</tt> and <tt class="literal">$b</tt> have to evaluate totrue or false in a Boolean context, and the existing bitwise operatordoesn't provide a Boolean context without help.</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_19.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_21.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">3.19. List Operators (Rightward)</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.21. C Operators Missing from Perl</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 + -