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

📄 ch04_05.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<h3 class="sect2">4.5.5. Autoincrement and Autodecrement</h3><p><a name="INDEX-489" /><a name="INDEX-490" /><a name="INDEX-491" /><a name="INDEX-492" /><a name="INDEX-493" /><a name="INDEX-494" />If placed before avariable, the <tt class="literal">++</tt> and <tt class="literal">--</tt>operators increment or decrement the variable before returning thevalue. If placed after, they increment or decrement the variableafter returning the value.</p></div><a name="perlnut2-CHP-4-SECT-5.6" /><div class="sect2"><h3 class="sect2">4.5.6. Assignment Operators</h3><p><a name="INDEX-495" />Perlrecognizes the following operators for assigning a value to avariable:</p><blockquote><pre class="code">=    **=    +=    *=    &amp;=    &lt;&lt;=    &amp;&amp;=-=    /=    |=    &gt;&gt;=   ||=    .=     %=^=    x=</pre></blockquote><p>Each operator requires a variable on the left side and an expressionon the right side. For the simple <a name="INDEX-496" /><a name="INDEX-497" /><a name="INDEX-498" /><a name="INDEX-499" /><a name="INDEX-500" /><a name="INDEX-501" /><a name="INDEX-502" /><a name="INDEX-503" /><a name="INDEX-504" /><a name="INDEX-505" /><a name="INDEX-506" /><a name="INDEX-507" /><a name="INDEX-508" /><a name="INDEX-509" /><a name="INDEX-510" /><a name="INDEX-511" /> <a name="INDEX-512" /><a name="INDEX-513" /><a name="INDEX-514" /><a name="INDEX-515" /><a name="INDEX-516" /><a name="INDEX-517" /><a name="INDEX-518" /><a name="INDEX-519" /><a name="INDEX-520" /><a name="INDEX-521" /><a name="INDEX-522" /><a name="INDEX-523" /><a name="INDEX-524" /><a name="INDEX-525" />assignmentoperator, <tt class="literal">=</tt>, the value of the expression is storedin the designated variable. For the other operators, Perl evaluatesthe expression:</p><blockquote><pre class="code">$var <em class="replaceable"><tt>OP</tt></em>= $value</pre></blockquote><p>as if it was written: </p><blockquote><pre class="code">$var = $var <em class="replaceable"><tt>OP</tt></em> $value</pre></blockquote><p>except that <tt class="literal">$var</tt> is evaluated only once. Forexample:</p><blockquote><pre class="code">$a += 2;    # Same as $a = $a + 2</pre></blockquote></div><a name="perlnut2-CHP-4-SECT-5.7" /><div class="sect2"><h3 class="sect2">4.5.7. Pattern Match Operators</h3><p><a name="INDEX-526" /><a name="INDEX-527" /> <a name="INDEX-528" />Binary <tt class="literal">=~</tt>binds a scalar expression to a pattern match, substitution, ortranslation. These operations search or modify the string<tt class="literal">$_</tt> by default.</p><p><a name="INDEX-529" /> Binary<tt class="literal">!~</tt> is just like <tt class="literal">=~</tt> except thereturn value is negated in the logical sense. The followingexpressions are functionally equivalent:</p><blockquote><pre class="code">$string !~ /<em class="replaceable"><tt>pattern</tt></em>/not $string =~ /<em class="replaceable"><tt>pattern</tt></em>/</pre></blockquote><p>see <a href="ch04_06.htm#perlnut2-CHP-4-SECT-6">Section 4.6, "Regular Expressions"</a> laterin this chapter.</p></div><a name="perlnut2-CHP-4-SECT-5.8" /><div class="sect2"><h3 class="sect2">4.5.8. File Test Operators</h3><p><a name="INDEX-530" /><a name="INDEX-531" /><a name="INDEX-532" /><a name="INDEX-533" /><a name="INDEX-534" /><a name="INDEX-535" /><a name="INDEX-536" /><a name="INDEX-537" /><a name="INDEX-538" /><a name="INDEX-539" /><a name="INDEX-540" /><a name="INDEX-541" /><a name="INDEX-542" /><a name="INDEX-543" /><a name="INDEX-544" /><a name="INDEX-545" /><a name="INDEX-546" /><a name="INDEX-547" /><a name="INDEX-548" /><a name="INDEX-549" /><a name="INDEX-550" /><a name="INDEX-551" /><a name="INDEX-552" /><a name="INDEX-553" /><a name="INDEX-554" /><a name="INDEX-555" /><a name="INDEX-556" /><a name="INDEX-557" />A file testoperator is a unary operator that tests a filename or a filehandle.</p><a name="ch04-4-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Operator</p></th><th><p>Meaning</p></th></tr><tr><td><p><tt class="literal">-r</tt></p></td><td><p>File is readable by effective uid/gid</p></td></tr><tr><td><p><tt class="literal">-w</tt></p></td><td><p>File is writable by effective uid/gid</p></td></tr><tr><td><p><tt class="literal">-x</tt></p></td><td><p>File is executable by effective uid/gid</p></td></tr><tr><td><p><tt class="literal">-o</tt></p></td><td><p>File is owned by effective uid</p></td></tr><tr><td><p><tt class="literal">-R</tt></p></td><td><p>File is readable by real uid/gid</p></td></tr><tr><td><p><tt class="literal">-W</tt></p></td><td><p>File is writable by real uid/gid</p></td></tr><tr><td><p><tt class="literal">-X</tt></p></td><td><p>File is executable by real uid/gid</p></td></tr><tr><td><p><tt class="literal">-O</tt></p></td><td><p>File is owned by real uid</p></td></tr><tr><td><p><tt class="literal">-e</tt></p></td><td><p>File exists</p></td></tr><tr><td><p><tt class="literal">-z</tt></p></td><td><p>File has zero size</p></td></tr><tr><td><p><tt class="literal">-s</tt></p></td><td><p>File has nonzero size (returns size)</p></td></tr><tr><td><p><tt class="literal">-f</tt></p></td><td><p>File is a plain file</p></td></tr><tr><td><p><tt class="literal">-d</tt></p></td><td><p>File is a directory</p></td></tr><tr><td><p><tt class="literal">-l</tt></p></td><td><p>File is a symbolic link</p></td></tr><tr><td><p><tt class="literal">-p</tt></p></td><td><p>File is a named pipe (FIFO)</p></td></tr><tr><td><p><tt class="literal">-S</tt></p></td><td><p>File is a socket</p></td></tr><tr><td><p><tt class="literal">-b</tt></p></td><td><p>File is a block special file</p></td></tr><tr><td><p><tt class="literal">-c</tt></p></td><td><p>File is a character special file</p></td></tr><tr><td><p><tt class="literal">-t</tt></p></td><td><p>Filehandle is opened to a tty</p></td></tr><tr><td><p><tt class="literal">-u</tt></p></td><td><p>File has setuid bit set</p></td></tr><tr><td><p><tt class="literal">-g</tt></p></td><td><p>File has setgid bit set</p></td></tr><tr><td><p><tt class="literal">-k</tt></p></td><td><p>File has sticky bit set</p></td></tr><tr><td><p><tt class="literal">-T</tt></p></td><td><p>File is a text file</p></td></tr><tr><td><p><tt class="literal">-B</tt></p></td><td><p>File is a binary file (opposite of <tt class="literal">-T</tt>)</p></td></tr><tr><td><p><tt class="literal">-M</tt></p></td><td><p>Age of file (at startup) in days since modification</p></td></tr><tr><td><p><tt class="literal">-A</tt></p></td><td><p>Age of file (at startup) in days since last access</p></td></tr><tr><td><p><tt class="literal">-C</tt></p></td><td><p>Age of file (at startup) in days since inode change</p></td></tr></table><p></div><a name="perlnut2-CHP-4-SECT-5.9" /><div class="sect2"><h3 class="sect2">4.5.9. Logical Operators</h3><p><a name="INDEX-558" /><a name="INDEX-559" /><a name="INDEX-560" /><a name="INDEX-561" /><a name="INDEX-562" /><a name="INDEX-563" /><a name="INDEX-564" /><a name="INDEX-565" />Perl provides the<tt class="literal">&amp;&amp;</tt> (logical AND) and <tt class="literal">||</tt>(logical OR) operators. They evaluate from left to right testing thetruth of the statement.</p><a name="ch04-5-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Example</p></th><th><p>Name</p></th><th><p>Result</p></th></tr><tr><td><p><tt class="literal">$a &amp;&amp; $b</tt></p></td><td><p>And</p></td><td><p><tt class="literal">$a</tt> if <tt class="literal">$a</tt> is false,<tt class="literal">$b</tt> otherwise</p></td></tr><tr><td><p><tt class="literal">$a || $b</tt></p></td><td><p>Or</p></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><p>For example, an oft-appearing idiom in Perl programs is: </p><blockquote><pre class="code">open(FILE, "somefile") || die "Cannot open somefile: $!\n";</pre></blockquote><p>In this case, Perl first evaluates the <tt class="literal">open</tt>function. If the value is true (because <tt class="literal">somefile</tt>was successfully opened), the execution of the <tt class="literal">die</tt>function is unnecessary and is skipped.</p><p>Perl also provides lower-precedence <tt class="literal">and</tt> and<tt class="literal">or</tt> operators that are more readable.</p></div><a name="perlnut2-CHP-4-SECT-5.10" /><div class="sect2"><h3 class="sect2">4.5.10. Bitwise Operators</h3><p><a name="INDEX-566" /><a name="INDEX-567" /><a name="INDEX-568" /><a name="INDEX-569" /><a name="INDEX-570" /><a name="INDEX-571" /><a name="INDEX-572" /><a name="INDEX-573" /><a name="INDEX-574" /><a name="INDEX-575" />Perl has bitwise AND, OR, and XOR(exclusive OR) operators: <tt class="literal">&amp;</tt>,<tt class="literal">|</tt>, and <tt class="literal">^</tt>. These operators workdifferently on numeric values than they do on strings. If eitheroperand is a number, then both operands are converted to integers,and the bitwise operation is performed between the two integers. Ifboth operands are strings, these operators do bitwise operationsbetween corresponding bits from the two strings.</p></div><a name="perlnut2-CHP-4-SECT-5.11" /><div class="sect2"><h3 class="sect2">4.5.11. Miscellaneous Operators</h3><p>The following operators don't fit into any of theprevious categories.</p><a name="perlnut2-CHP-4-SECT-5.11.1" /><div class="sect3"><h3 class="sect3">4.5.11.1. Range operator</h3><p><a name="INDEX-576" /><a name="INDEX-577" /><a name="INDEX-578" />The <tt class="literal">..</tt> rangeoperator is really two different operators depending on the context.In a list context, it returns a list of values counting (by ones)from the left value to the right value.</p><p>In a scalar context, <tt class="literal">..</tt> returns a Boolean value.It is false as long as its left operand is false. Once the leftoperand is true, the range operator stays true until the rightoperand is true, after which the range operator becomes false again.The right operand is not evaluated while the operator is in the falsestate, and the left operand is not evaluated while the operator is inthe true state.</p><p><a name="INDEX-579" /><a name="INDEX-580" /><a name="INDEX-581" />The alternate version of thisoperator, <tt class="literal">...</tt>, does not test the right operandimmediately when the operator becomes true; it waits until the nextevaluation.</p></div><a name="perlnut2-CHP-4-SECT-5.11.2" /><div class="sect3"><h3 class="sect3">4.5.11.2. Conditional operator</h3><p><a name="INDEX-582" /><a name="INDEX-583" /><a name="INDEX-584" />Ternary <tt class="literal">?</tt>: is theconditional operator. It works like an if-then-else statement but cansafely be embedded within other operations and functions:</p><blockquote><pre class="code"><em class="replaceable"><tt>test_expr</em> ? <em class="replaceable">if_true_expr</em> : <em class="replaceable">if_false_expr</tt></em></pre></blockquote><p>If the <em class="replaceable"><tt>test_expr</tt></em> is true, only the<em class="replaceable"><tt>if_true_expr</tt></em> is evaluated. Otherwise, onlythe <em class="replaceable"><tt>if_false_expr</tt></em> is evaluated. Eitherway, the value of the evaluated expression becomes the value of theentire expression.</p></div><a name="perlnut2-CHP-4-SECT-5.11.3" /><div class="sect3"><h3 class="sect3">4.5.11.3. Comma operator</h3><p><a name="INDEX-585" /><a name="INDEX-586" />In a listcontext, <tt class="literal">,</tt> is the list argument separator andinserts both its arguments into the list. In scalar context,<tt class="literal">,</tt> evaluates its left argument, throws that valueaway, then evaluates its right argument and returns that value.</p><p><a name="INDEX-587" /><a name="INDEX-588" />The<tt class="literal">=&gt;</tt> operator is mostly just a synonym for thecomma operator. It's useful for documentingarguments that come in pairs. It also forces any identifier to theleft of it to be interpreted as a string.</p></div><a name="perlnut2-CHP-4-SECT-5.11.4" /><div class="sect3"><h3 class="sect3">4.5.11.4. String operator</h3><p><a name="INDEX-589" /><a name="INDEX-590" /><a name="INDEX-591" /><a name="INDEX-592" />The concatenation operator<tt class="literal">.</tt> is used to add strings together:</p><blockquote><pre class="code">print 'abc' . 'def';        # Prints abcdefprint $a . $b;              # Concatenates the string values of $a and $b</pre></blockquote><p><a name="INDEX-593" /><a name="INDEX-594" />Binary <tt class="literal">x</tt> is thestring repetition operator. In scalar context, it returns aconcatenated string consisting of the left operand repeated thenumber of times specified by the right operand:</p><blockquote><pre class="code">print '-' x 80;                           # Prints row of dashesprint "\t" x ($tab/8), ' ' x ($tab%8);    # Tabs over</pre></blockquote><p>In list context, if the left operand is a list in parentheses, the<tt class="literal">x</tt> works as a list replicator rather than a stringreplicator. This is useful for initializing all the elements of anarray of indeterminate length to the same value:</p><blockquote><pre class="code">@ones = (1) x 80;           # A list of 80 1s@ones = (5) x @ones;        # Set all elements to 5</pre></blockquote></div></div><hr width="684" align="left" /><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"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch04_06.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">4.4. Special Variables</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">4.6. Regular Expressions</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>

⌨️ 快捷键说明

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