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

📄 ch03_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 2 页
字号:
defined usually follow your intuition, presuming you're not psychotic.And second, if you're merely neurotic, you can always put in extraparentheses to relieve your anxiety.</p><p><a name="INDEX-788"></a><a name="INDEX-789"></a>Another helpful hint is that any operators borrowed from C keep thesame precedence relationship with each other, even where C's precedenceis slightly screwy. (This makes learning Perl easier for C folks andC++ folks.  Maybe even Java folks.)</p><p><a name="INDEX-790"></a>The following sections cover these operators in precedenceorder.  With very few exceptions, these all operate on scalar valuesonly, not list values.  We'll mention the exceptions as they come up.</p><p><a name="INDEX-791"></a><a name="INDEX-792"></a>Although references are scalar values, using most of these operatorson references doesn't make much sense, because the numeric value ofa reference is only meaningful to the internals of Perl.  Nevertheless,if a reference points to an object of a class that allows overloading,you can call these operators on such objects, and if the class hasdefined an overloading for that particular operator, it will definehow the object is to be treated under that operator.  This is howcomplex numbers are implemented in Perl, for instance.  For more onoverloading, see <a href="ch13_01.htm">Chapter 13, "Overloading"</a>.</p><h2 class="sect1">3.1. Terms and List Operators (Leftward)</h2><a name="INDEX-793"></a><a name="INDEX-794"></a><a name="INDEX-795"></a><a name="INDEX-796"></a><a name="INDEX-797"></a><a name="INDEX-798"></a><a name="INDEX-799"></a><a name="INDEX-800"></a><p>Any <em class="emphasis">term</em> is of highest precedence in Perl.  Termsinclude variables, quote and quotelike operators, most expressions inparentheses, or brackets or braces, and any function whose argumentsare parenthesized.  Actually, there aren't really any functions inthis sense, just list operators and unary operators behaving asfunctions because you put parentheses around theirarguments. Nevertheless, the name of Chapter 29 is<em class="emphasis">Functions</em>.</p><p><a name="INDEX-801"></a>Now listen carefully.  Here are a couple of rules that are veryimportant and simplify things greatly, but may occasionally producecounterintuitive results for the unwary.  If any list operator (suchas <tt class="literal">print</tt>) or any named unary operator (such as<tt class="literal">chdir</tt>) is followed by a left parenthesis as thenext token (ignoring whitespace), the operator and its parenthesizedarguments are given highest precedence, as if it were a normalfunction call.  The rule is this: If it <em class="emphasis">looks</em>like a function call, it <em class="emphasis">is</em> a function call. Youcan make it look like a nonfunction by prefixing the parentheses witha unary plus, which does absolutely nothing, semantically speaking--itdoesn't even coerce the argument to be numeric.<a name="INDEX-802"></a></p><p><a name="INDEX-803"></a><a name="INDEX-804"></a><a name="INDEX-805"></a><a name="INDEX-806"></a><a name="INDEX-807"></a>For example, since || has lower precedence than <tt class="literal">chdir</tt>, we get:<blockquote><pre class="programlisting">chdir $foo    || die;       # (chdir $foo) || diechdir($foo)   || die;       # (chdir $foo) || diechdir ($foo)  || die;       # (chdir $foo) || diechdir +($foo) || die;       # (chdir $foo) || die</pre></blockquote>but, because <tt class="literal">*</tt> has higher precedence than <tt class="literal">chdir</tt>, we get:<blockquote><pre class="programlisting">chdir $foo * 20;            # chdir ($foo * 20)chdir($foo) * 20;           # (chdir $foo) * 20chdir ($foo) * 20;          # (chdir $foo) * 20chdir +($foo) * 20;         # chdir ($foo * 20)</pre></blockquote>Likewise for any numeric operator that happens to be a named unary operator,such as <tt class="literal">rand</tt>:<blockquote><pre class="programlisting">rand 10 * 20;               # rand (10 * 20)rand(10) * 20;              # (rand 10) * 20rand (10) * 20;             # (rand 10) * 20rand +(10) * 20;            # rand (10 * 20)</pre></blockquote>In the absence of parentheses, the precedence of list operators such as<tt class="literal">print</tt>, <tt class="literal">sort</tt>, or <tt class="literal">chmod</tt> is either very high or very low dependingon whether you look at the left side or the right side of the operator.(That's what the "Leftward" is doing in the title of thissection.)  For example, in:<blockquote><pre class="programlisting">@ary = (1, 3, sort 4, 2);print @ary;         # prints 1324</pre></blockquote>the commas on the right of the <tt class="literal">sort</tt> are evaluatedbefore the <tt class="literal">sort</tt>, but the commas on the left areevaluated after.  In other words, a list operator tends to gobble upall the arguments that follow it, and then act like a simple term withregard to the preceding expression.  You still have to be careful withparentheses:<blockquote><pre class="programlisting"># These evaluate exit before doing the print:print($foo, exit);  # Obviously not what you want.print $foo, exit;   # Nor this.# These do the print before evaluating exit:(print $foo), exit; # This is what you want.print($foo), exit;  # Or this.print ($foo), exit; # Or even this.</pre></blockquote>The easiest place to get burned is where you're using parentheses togroup mathematical arguments, and you forget that parentheses are alsoused to group function arguments:<blockquote><pre class="programlisting">print ($foo &amp; 255) + 1, "\n";   # prints ($foo &amp; 255)</pre></blockquote>That probably doesn't do what you expect at first glance.  Fortunately,mistakes of this nature generally produce warnings like "<tt class="literal">Useless use of addition in a void context</tt>" when warnings are enabled.</p><p><a name="INDEX-808"></a><a name="INDEX-809"></a>Also parsed as terms are the <tt class="literal">do {}</tt> and <tt class="literal">eval {}</tt> constructs, as wellas subroutine and method calls, the anonymous array and hash composers<tt class="literal">[]</tt> and <tt class="literal">{}</tt>, and the anonymous subroutine composer <tt class="literal">sub {}</tt>.</p><a name="INDEX-1027"></a><a name="INDEX-1028"></a><a name="INDEX-1029"></a><!-- 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="ch02_11.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_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">2.11. Input 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.2. The Arrow 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 + -