📄 ch01_05.htm
字号:
If you place one of these "auto" operators before the variable, it is knownas a pre-incremented (pre-decremented) variable. Its value will bechanged before it is referenced. If it is placed after the variable,it is known as a post-incremented (post-decremented) variable, and itsvalue is changed after it is used. For example:<blockquote><pre class="programlisting">$a = 5; # $a is assigned 5$b = ++$a; # $b is assigned the incremented value of $a, 6$c = $a--; # $c is assigned 6, then $a is decremented to 5</pre></blockquote>Line 15 of our Average Example increments the number of scores by one,so that we'll know how many scores we're averaging. It uses apost-increment operator (<tt class="literal">$scores++</tt>), but in thiscase it doesn't matter, since the expression is in a void context,which is just a funny way of saying that the expression is beingevaluated only for the side effect of incrementing the variable. Thevalue returned is being thrown away.<a href="#FOOTNOTE-18">[18]</a><a name="INDEX-235"></a></p><blockquote class="footnote"><a name="FOOTNOTE-18"></a><p>[18] The optimizerwill notice this and optimize the post-increment into a pre-increment,because that's a bit faster to execute. (You didn't need to knowthat, but we hoped it would cheer you up.)</p></blockquote><h3 class="sect2">1.5.5. Logical Operators</h3><p><a name="INDEX-236"></a><a name="INDEX-237"></a><a name="INDEX-238"></a><a name="INDEX-239"></a>Logical operators, also known as "short-circuit" operators, allow theprogram to make decisions based on multiple criteria without usingnested <tt class="literal">if</tt> statements. They are known asshort-circuit operators because they skip (short circuit) theevaluation of their right argument if they decide the left argumenthas already supplied enough information to decide the overallvalue. This is not just for efficiency. You are explicitly allowed todepend on this short-circuiting behavior to avoid evaluating code inthe right argument that you know would blow up if the left argumentwere not "guarding" it. You can say "California or bust!" in Perlwithout busting (presuming you do get to California).</p><p><a name="INDEX-240"></a><a name="INDEX-241"></a>Perl actually has two sets of logical operators, a traditional setborrowed from C and a newer (but even more traditional) set ofultralow-precedence operators borrowed from BASIC. Both sets contributeto readability when used appropriately. C's punctuational operatorswork well when you want your logical operators to bind more tightly thancommas, while BASIC's word-based operators work well when you want yourcommas to bind more tightly than your logical operators. Often they workthe same, and which set you use is a matter of personal preference. (Forcontrastive examples, see <a href="ch03_20.htm#ch03-sect-logic">Section 1.20, "Logical and, or, not, and xor"</a> in <a href="ch03_01.htm">Chapter 3, "Unary and Binary Operators"</a>.) Although thetwo sets of operators are not interchangeable due to precedence, oncethey're parsed, the operators themselves behave identically;precedence merely governs the extent of their arguments. <a href="ch01_05.htm#perl3-tab-over-logical">Table 1-1</a> lists logical operators.</p><a name="perl3-tab-over-logical"></a><h4 class="objtitle">Table 1.1. Logical Operators</h4><table border="1"><tr><th>Example</th><th>Name</th><th>Result</th></tr><tr><td><tt class="literal">$a && $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<a name="INDEX-242"></a><a name="INDEX-243"></a></td></tr><tr><td><tt class="literal">$a || $b</tt></td><td>Or</td><td><tt class="literal">$a</tt> if <tt class="literal">$a</tt> is true, <tt class="literal">$b</tt> otherwise<a name="INDEX-244"></a><a name="INDEX-245"></a></td></tr><tr><td><tt class="literal">! $a</tt></td><td>Not</td><td>True if <tt class="literal">$a</tt> is not true</td></tr><tr><td><tt class="literal">$a and $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 or $b</tt></td><td>Or</td><td><tt class="literal">$a</tt> if <tt class="literal">$a</tt> is true, <tt class="literal">$b</tt> otherwise<a name="INDEX-246"></a></td></tr><tr><td><tt class="literal">not $a</tt></td><td>Not</td><td>True if <tt class="literal">$a</tt> is not true</td></tr><tr><td><tt class="literal">$a xor $b</tt></td><td>Xor</td><td>True if <tt class="literal">$a</tt> or <tt class="literal">$b</tt> is true, but not both<a name="INDEX-247"></a></td></tr></table><p><a name="INDEX-248"></a><a name="INDEX-249"></a>Since the logical operators "short-circuit" the way they do, they'reoften used in Perl to conditionally execute code. The following line(line 3 from our Average Example) tries to open the file<em class="emphasis">grades</em>:<blockquote><pre class="programlisting">open(GRADES, "grades") or die "Can't open file grades: $!\n";</pre></blockquote>If it opens the file, it will jump to the next line of the program. Ifit can't open the file, it will provide us with an error message andthen stop execution.</p><p><a name="INDEX-250"></a>Literally, this line means "Open <em class="emphasis">grades</em> or bust!"Besides being another example of natural language, the short-circuitoperators preserve the visual flow. Important actions are listed downthe left side of the screen, and secondary actions are hidden off tothe right. (The <tt class="literal">$!</tt> variable contains the errormessage returned by the operating system--see<a href="ch28_01.htm">Chapter 28, "Special Names"</a>.) Of course, these logicaloperators can also be used within the more traditional kinds ofconditional constructs, such as the <tt class="literal">if</tt> and<tt class="literal">while</tt> statements.<a name="INDEX-251"></a><a name="INDEX-252"></a></p><h3 class="sect2">1.5.6. Some Numeric and String Comparison Operators</h3><p><a name="INDEX-253"></a><a name="INDEX-254"></a><a name="INDEX-255"></a><a name="INDEX-256"></a></p><p>Comparison, or relational, operators tell us how two scalar values(numbers or strings) relate to each other. There are two sets ofoperators; one does numeric comparison and the other does stringcomparison. (In either case, the arguments will be "coerced" to havethe appropriate type first.) Assuming left and right arguments of<tt class="literal">$a</tt> and <tt class="literal">$b</tt>, we have:</p><a name="perl3-tab-over-compare"></a><table border="1"><tr><th>Comparison</th><th>Numeric</th><th>String</th><th>Return Value</th></tr><tr><td>Equal</td><td><tt class="literal">==</tt></td><td><tt class="literal">eq</tt></td><td>True if <tt class="literal">$a</tt> is equal to <tt class="literal">$b</tt><a name="INDEX-257"></a></td></tr><tr><td>Not equal</td><td><tt class="literal">!=</tt></td><td><tt class="literal">ne</tt></td><td>True if <tt class="literal">$a</tt> is not equal to <tt class="literal">$b</tt><a name="INDEX-258"></a></td></tr><tr><td>Less than</td><td><tt class="literal"><</tt></td><td><tt class="literal">lt</tt></td><td>True if <tt class="literal">$a</tt> is less than <tt class="literal">$b</tt><a name="INDEX-259"></a></td></tr><tr><td>Greater than</td><td><tt class="literal">></tt></td><td><tt class="literal">gt</tt></td><td>True if <tt class="literal">$a</tt> is greater than <tt class="literal">$b</tt><a name="INDEX-260"></a></td></tr><tr><td>Less than or equal</td><td><tt class="literal"><=</tt></td><td><tt class="literal">le</tt></td><td>True if <tt class="literal">$a</tt> not greater than <tt class="literal">$b</tt><a name="INDEX-261"></a></td></tr><tr><td>Comparison</td><td><tt class="literal"><=></tt></td><td><tt class="literal">cmp</tt></td><td>0 if equal, 1 if <tt class="literal">$a</tt> greater, -1 if <tt class="literal">$b</tt> greater<a name="INDEX-262"></a><a name="INDEX-263"></a><a name="INDEX-264"></a></td></tr></table><p><a name="INDEX-265"></a><a name="INDEX-266"></a>The last pair of operators (<tt class="literal"><=></tt> and<tt class="literal">cmp</tt>) are entirely redundant. However, they'reincredibly useful in <tt class="literal">sort</tt> subroutines (see<a href="ch29_01.htm">Chapter 29, "Functions"</a>).<a href="#FOOTNOTE-19">[19]</a></p><blockquote class="footnote"><a name="FOOTNOTE-19"></a><p>[19] Some folks feelthat such redundancy is evil because it keeps a language from beingminimalistic, or orthogonal. But Perl isn't an orthogonal language;it's a diagonal language. By this we mean that Perl doesn't force youto always go at right angles. Sometimes you just want to follow thehypotenuse of the triangle to get where you're going. TMTOWTDI isabout shortcuts. Shortcuts are about efficiency.</p></blockquote><h3 class="sect2">1.5.7. Some File Test Operators</h3><p><a name="INDEX-267"></a><a name="INDEX-268"></a><a name="INDEX-269"></a>The file test operators allow you to test whether certain fileattributes are set before you go and blindly muck about with the files.The most basic file attribute is, of course, whether the file exists.For example, it would be very nice to know whether your mail aliasesfile already exists before you go and open it as a new file, wiping outeverything that was in there before. Here are a few of thefile test operators:</p><a name="perl3-tab-over-file"></a><table border="1"><tr><th>Example</th><th>Name</th><th>Result</th></tr><tr><td><tt class="literal">-e $a</tt></td><td>Exists</td><td>True if file named in <tt class="literal">$a</tt> exists<a name="INDEX-270"></a></td></tr><tr><td><tt class="literal">-r $a</tt></td><td>Readable</td><td>True if file named in <tt class="literal">$a</tt> is readable<a name="INDEX-271"></a><a name="INDEX-272"></a></td></tr><tr><td><tt class="literal">-w $a</tt></td><td>Writable</td><td>True if file named in <tt class="literal">$a</tt> is writable<a name="INDEX-273"></a><a name="INDEX-274"></a></td></tr><tr><td><tt class="literal">-d $a</tt></td><td>Directory</td><td>True if file named in <tt class="literal">$a</tt> is a directory<a name="INDEX-275"></a><a name="INDEX-276"></a></td></tr><tr><td><tt class="literal">-f $a</tt></td><td>File</td><td>True if file named in <tt class="literal">$a</tt> is a regular file<a name="INDEX-277"></a><a name="INDEX-278"></a></td></tr><tr><td><tt class="literal">-T $a</tt></td><td>Text File</td><td>True if file named in <tt class="literal">$a</tt> is a text file<a name="INDEX-279"></a><a name="INDEX-280"></a></td></tr></table><p>You might use them like this:<blockquote><pre class="programlisting">-e "/usr/bin/perl" or warn "Perl is improperly installed\n";-f "/vmlinuz" and print "I see you are a friend of Linus\n";</pre></blockquote>Note that a regular file is not the same thing as a text file. Binaryfiles like <em class="emphasis">/vmlinuz</em> are regular files, but theyaren't text files. Text files are the opposite of binary files, whileregular files are the opposite of "irregular"files like directoriesand devices. <a name="INDEX-281"></a><a name="INDEX-282"></a></p><p><a name="INDEX-283"></a><a name="INDEX-284"></a>There are a lot of file test operators, many of which we didn't list.Most of the file tests are unary Boolean operators, which is to saythey take only one operand (a scalar that evaluates to a filename or afilehandle), and they return either a true or false value. A few ofthem return something fancier, like the file's size or age, but you canlook those up when you need them in the section <a href="ch03_10.htm#ch03-sect-nu">Section 1.10, "Named Unary and File Test Operators"</a> in <a href="ch03_01.htm">Chapter 3, "Unary and Binary Operators"</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="ch01_04.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="ch01_06.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">1.4. Filehandles</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">1.6. Control Structures</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 © 2001</a> O'Reilly & 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 + -