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

📄 ch04_05.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>Operators (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><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="ch04_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch04_06.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">4.5. Operators</h2><p><a name="INDEX-427" /><a name="INDEX-428" /><a href="ch04_05.htm#perlnut2-CHP-4-TABLE-3">Table 4-3</a> lists allthe Perl operators from highest to lowest precedence and indicatestheir associativity.</p><a name="perlnut2-CHP-4-TABLE-3" /><h4 class="objtitle">Table 4-3. Perl associativity and operators, listed by precedence </h4><table border="1" cellpadding="3"><tr><th><p>Associativity</p></th><th><p>Operators</p></th></tr><tr><td><p>Left</p></td><td><p>Terms and list operators (leftward)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">-&gt;</tt> (method call, dereference)</p></td></tr><tr><td><p>Nonassociative</p></td><td><p><tt class="literal">++ --</tt> (autoincrement, autodecrement)</p></td></tr><tr><td><p>Right</p></td><td><p><tt class="literal">**</tt> (exponentiation)</p></td></tr><tr><td><p>Right</p></td><td><p><tt class="literal">! ~ \</tt> and unary <tt class="literal">+</tt> and<tt class="literal">-</tt> (logical not, bit-not, reference, unary plus,unary minus)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">=~ !~</tt> (matches, doesn't match)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">* / % x</tt> (multiply, divide, modulus, stringreplicate)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">+ -</tt> . (addition, subtraction, string concatenation)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">&lt;&lt; &gt;&gt;</tt> (left bit-shift, right bit-shift)</p></td></tr><tr><td><p>Nonassociative</p></td><td><p>Named unary operators and file-test operators</p></td></tr><tr><td><p>Nonassociative</p></td><td><p><tt class="literal">&lt; &gt; &lt;= &gt;= lt gt le ge</tt> (less than,greater than, less than or equal to, greater than or equal to, andtheir string equivalents)</p></td></tr><tr><td><p>Nonassociative</p></td><td><p><tt class="literal">== != &lt;=&gt; eq ne cmp</tt> (equal to, not equal to,signed comparison, and their string equivalents)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">&amp;</tt> (bit-and)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">| ^</tt> (bit-or, bit-xor)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">&amp;&amp;</tt> (logical AND)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">||</tt> (logical OR)</p></td></tr><tr><td><p>Nonassociative</p></td><td><p><tt class="literal">.. ...</tt> (range)</p></td></tr><tr><td><p>Right</p></td><td><p><tt class="literal">?</tt>: (ternary conditional)</p></td></tr><tr><td><p>Right</p></td><td><p><tt class="literal">= += -= *=</tt> and so on (assignment operators)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">, =&gt;</tt> (comma, arrow comma)</p></td></tr><tr><td><p>Nonassociative</p></td><td><p>List operators (rightward)</p></td></tr><tr><td><p>Right</p></td><td><p><tt class="literal">not</tt> (logical not)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">and</tt> (logical and)</p></td></tr><tr><td><p>Left</p></td><td><p><tt class="literal">or xor</tt> (logical or, xor)</p></td></tr></table><p><p><a name="INDEX-429" /><a name="INDEX-430" />You can clarify your expressions by usingparentheses to group any part of an expression. Anything inparentheses will be evaluated as a single unit within a largerexpression.</p><p>With very few exceptions, Perl operators act upon scalar values only,not upon list values.</p><p>Terms that take highest precedence in Perl include variables, quoteand quotelike operators, any expression in parentheses, and anyfunction with arguments in parentheses.</p><p><a name="INDEX-431" />A listoperator is a function that can take a list of values as itsargument. List operators take highest precedence when consideringwhat's to the left of them. They have considerablylower precedence when looking to their right, which is the expectedresult.</p><p>Also parsed as high-precedence terms are the <tt class="literal">do{}</tt>and <tt class="literal">eval{}</tt> constructs, as well as subroutine andmethod calls, the anonymous array and hash composers(<tt class="literal">[]</tt> and <tt class="literal">{}</tt>), and the anonymoussubroutine composer <tt class="literal">sub{}</tt>.</p><p>A unary operator is a function that takes a single scalar value asits argument. Unary operators have a lower precedence than listoperators because they expect and take only one value.</p><a name="perlnut2-CHP-4-SECT-5.1" /><div class="sect2"><h3 class="sect2">4.5.1. The Arrow Operator</h3><p><a name="INDEX-432" /><a name="INDEX-433" /><a name="INDEX-434" /><a name="INDEX-435" />The arrow operator is a dereferenceoperator. It can be used for references to arrays, hashes, codereferences, or for calling methods on objects. See the discussion ofreferences in <a href="ch07_01.htm">Chapter 7, "Packages, Modules, and Objects"</a>.</p></div><a name="perlnut2-CHP-4-SECT-5.2" /><div class="sect2"><h3 class="sect2">4.5.2. Unary Operators</h3><p><a name="INDEX-436" /><a name="INDEX-437" /><a name="INDEX-438" />Unary <tt class="literal">!</tt> performslogical negation, that is, "not."The <tt class="literal">not</tt> operator is a lower-precedence version of<tt class="literal">!</tt>.</p><p><a name="INDEX-439" /><a name="INDEX-440" />Unary <tt class="literal">-</tt> performsarithmetic negation if the operand is numeric. If the operand is anidentifier, then a string consisting of a minus sign concatenatedwith the identifier is returned. Otherwise, if the string starts witha plus or minus, a string starting with the opposite sign isreturned.</p><p><a name="INDEX-441" /><a name="INDEX-442" />Unary <tt class="literal">~</tt> performsbitwise negation, that is, one's complement. Forexample, on a 32-bit machine, <tt class="literal">~0xFF</tt> is<tt class="literal">0xFFFFFF00</tt>. If the argument to<tt class="literal">~</tt> is a string instead of a number, a string ofidentical length is returned, but with all the bits of the stringcomplemented.</p><p><a name="INDEX-443" /><a name="INDEX-444" />Unary<tt class="literal">+</tt> has no semantic effect whatsoever, even onstrings. It is syntactically useful for separating a function namefrom a parenthesized expression that would otherwise be interpretedas the complete list of function arguments.</p><p><a name="INDEX-445" /><a name="INDEX-446" />Unary <tt class="literal">\</tt> creates areference to whatever follows it (see <a href="ch04_08.htm#perlnut2-CHP-4-SECT-8">Section 4.8, "References and Complex Data Structures"</a> later in this chapter).Do not confuse this behavior with the behavior of the backslashwithin a string. The <tt class="literal">\</tt> operator may also be usedon a parenthesized list value in a list context, in which case itreturns references to each element of the list.</p></div><a name="perlnut2-CHP-4-SECT-5.3" /><div class="sect2"><h3 class="sect2">4.5.3. Arithmetic Operators</h3><p><a name="INDEX-447" /><a name="INDEX-448" /><a name="INDEX-449" /><a name="INDEX-450" />Binary <tt class="literal">**</tt> isthe exponentiation operator. Note that it binds even more tightlythan unary minus, so <tt class="literal">-2**4</tt> is<tt class="literal">-(2**4)</tt>, not <tt class="literal">(-2)**4</tt>. Also notethat <tt class="literal">**</tt> has right associativity, so:</p><blockquote><pre class="code">$e = 2 ** 3 ** 4;</pre></blockquote><p>evaluates to 2 to the 81st power, not 8 to the 4th power.</p><p><a name="INDEX-451" /><a name="INDEX-452" /><a name="INDEX-453" /><a name="INDEX-454" /><a name="INDEX-455" /><a name="INDEX-456" />The <tt class="literal">*</tt> (multiply) and<tt class="literal">/</tt> (divide) operators work exactly as you mightexpect, multiplying or dividing their two operands. Division is donein floating-point mode, unless integer mode in enabled (via<tt class="literal">use integer</tt>).</p><p><a name="INDEX-457" /><a name="INDEX-458" /><a name="INDEX-459" />The <tt class="literal">%</tt> (modulus)operator converts its operands to integers before finding theremainder according to integer division. For the same operation infloating-point mode, you may prefer to use the <tt class="literal">fmod()</tt> function from the POSIX module (see <a href="ch08_01.htm">Chapter 8, "Standard Modules"</a>). </p></div><a name="perlnut2-CHP-4-SECT-5.4" /><div class="sect2"><h3 class="sect2">4.5.4. Comparison Operators</h3><p>Comparison operators can be categorized into relational and equalityoperators.</p><a name="perlnut2-CHP-4-SECT-5.4.1" /><div class="sect3"><h3 class="sect3">4.5.4.1. Relational operators</h3><p><a name="INDEX-460" /><a name="INDEX-461" /><a name="INDEX-462" /><a name="INDEX-463" /><a name="INDEX-464" /><a name="INDEX-465" /><a name="INDEX-466" /><a name="INDEX-467" /><a name="INDEX-468" /><a name="INDEX-469" /><a name="INDEX-470" /><a name="INDEX-471" /><a name="INDEX-472" /><a name="INDEX-473" /><a name="INDEX-474" /><a name="INDEX-475" /><a name="INDEX-476" /><a name="INDEX-477" /><a name="INDEX-478" />Perl has two classes of relationaloperators. One class operates on numeric values, and the otheroperates on string values. String comparisons are based on the ASCIIcollating sequence. Relational operators are nonassociative, so<tt class="literal">$a &lt; $b &lt; $c</tt> is a syntax error.</p><a name="ch04-2-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Numeric</p></th><th><p>String</p></th><th><p>Meaning</p></th></tr><tr><td><p><tt class="literal">&gt;</tt></p></td><td><p><tt class="literal">gt</tt></p></td><td><p>Greater than</p></td></tr><tr><td><p><tt class="literal">&gt;=</tt></p></td><td><p><tt class="literal">ge</tt></p></td><td><p>Greater than or equal to</p></td></tr><tr><td><p><tt class="literal">&lt;</tt></p></td><td><p><tt class="literal">lt</tt></p></td><td><p>Less than</p></td></tr><tr><td><p><tt class="literal">&lt;=</tt></p></td><td><p><tt class="literal">le</tt></p></td><td><p>Less than or equal to</p></td></tr></table><p></div><a name="perlnut2-CHP-4-SECT-5.4.2" /><div class="sect3"><h3 class="sect3">4.5.4.2. Equality operators</h3><p><a name="INDEX-479" /><a name="INDEX-480" /><a name="INDEX-481" /><a name="INDEX-482" /><a name="INDEX-483" /><a name="INDEX-484" /><a name="INDEX-485" /><a name="INDEX-486" /><a name="INDEX-487" /><a name="INDEX-488" />The equal and not-equal operatorsreturn <tt class="literal">1</tt> for true and"" for false (just as therelational operators do). The <tt class="literal">&lt;=&gt;</tt> and<em class="emphasis">cmp</em> operators return <tt class="literal">-1</tt> ifthe left operand is less than the right operand, <tt class="literal">0</tt>if they are equal, and <tt class="literal">+1</tt> if the left operand isgreater than the right.</p><a name="ch04-3-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Numeric</p></th><th><p>String</p></th><th><p>Meaning</p></th></tr><tr><td><p><tt class="literal">==</tt></p></td><td><p><tt class="literal">eq</tt></p></td><td><p>Equal to</p></td></tr><tr><td><p><tt class="literal">!=</tt></p></td><td><p><tt class="literal">ne</tt></p></td><td><p>Not equal to</p></td></tr><tr><td><p><tt class="literal">&lt;=&gt;</tt></p></td><td><p><tt class="literal">cmp</tt></p></td><td><p>Comparison, with signed result</p></td></tr></table><p></div></div><a name="perlnut2-CHP-4-SECT-5.5" /><div class="sect2">

⌨️ 快捷键说明

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