📄 ch03_17.htm
字号:
<html><head><title>Assignment Operators (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 & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Assignment Operators"><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_16.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_18.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.17. Assignment Operators</h2><p><a name="INDEX-991"></a><a name="INDEX-992"></a><a name="INDEX-993"></a><a name="INDEX-994"></a><a name="INDEX-995"></a><a name="INDEX-996"></a><a name="INDEX-997"></a><a name="INDEX-998"></a><a name="INDEX-999"></a><a name="INDEX-1000"></a><a name="INDEX-1001"></a><a name="INDEX-1002"></a><a name="INDEX-1003"></a><a name="INDEX-1004"></a><a name="INDEX-1005"></a><a name="INDEX-1006"></a><a name="INDEX-1007"></a>Perl recognizes the C assignment operators, as well as providing some ofits own. There are quite a few of them:<blockquote><pre class="programlisting">= **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x=</pre></blockquote><a name="INDEX-1008"></a>Each operator requires a target lvalue (typically a variable or arrayelement) on the left side and an expression on the right side. Forthe simple assignment operator:<blockquote><pre class="programlisting"><em class="replaceable">TARGET</em> = <em class="replaceable">EXPR</em></pre></blockquote>the value of the <em class="replaceable">EXPR</em> is stored into the variable or locationdesignated by <em class="replaceable">TARGET</em>. For the other operators, Perl evaluates theexpression:<blockquote><pre class="programlisting"><em class="replaceable">TARGET</em> <em class="replaceable">OP</em>= <em class="replaceable">EXPR</em></pre></blockquote>as if it were written:<blockquote><pre class="programlisting"><em class="replaceable">TARGET</em> = <em class="replaceable">TARGET</em> <em class="replaceable">OP</em> <em class="replaceable">EXPR</em></pre></blockquote>That's a handy mental rule, but it's misleading in two ways. First,assignment operators always parse at the precedence level of ordinaryassignment, regardless of the precedence that <em class="replaceable">OP</em> would have byitself. Second, <em class="replaceable">TARGET</em> is evaluated only once. Usually thatdoesn't matter unless there are side effects, such as anautoincrement:<blockquote><pre class="programlisting">$var[$a++] += $value; # $a is incremented once$var[$a++] = $var[$a++] + $value; # $a is incremented twice</pre></blockquote>Unlike in C, the assignment operator produces a valid lvalue. Modifyingan assignment is equivalent to doing the assignment and then modifyingthe variable to which it was assigned. This is useful for modifying a copyof something, like this:<blockquote><pre class="programlisting">($tmp = $global) += $constant;</pre></blockquote>which is the equivalent of:<blockquote><pre class="programlisting">$tmp = $global + $constant;</pre></blockquote>Likewise:<blockquote><pre class="programlisting">($a += 2) *= 3;</pre></blockquote>is equivalent to:<blockquote><pre class="programlisting">$a += 2;$a *= 3;</pre></blockquote>That's not terribly useful, but here's an idiom you see frequently:<blockquote><pre class="programlisting">($new = $old) =~ s/foo/bar/g;</pre></blockquote>In all cases, the value of the assignment is the new value of thevariable. Since assignment operators associate right-to-left, this canbe used to assign many variables the same value, as in:<blockquote><pre class="programlisting">$a = $b = $c = 0;</pre></blockquote>which assigns <tt class="literal">0</tt> to <tt class="literal">$c</tt>, and the result of that (still <tt class="literal">0</tt>) to<tt class="literal">$b</tt>, and the result of that (<em class="emphasis">still</em><tt class="literal">0</tt>) to <tt class="literal">$a</tt>.</p><p><a name="INDEX-1009"></a><a name="INDEX-1010"></a>List assignment may be done only with the plain assignment operator,<tt class="literal">=</tt>. In list context, list assignment returns the list of new valuesjust as scalar assignment does. In scalar context, list assignmentreturns the number of values that were available on the right side ofthe assignment, as mentioned in <a href="ch02_01.htm">Chapter 2, "Bits and Pieces"</a>.This makes it useful for testing functions that return a null list when unsuccessful (or no longer successful), as in:<blockquote><pre class="programlisting">while (($key, $value) = each %gloss) { ... }next unless ($dev, $ino, $mode) = stat $file;</pre></blockquote></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_16.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_18.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">3.16. Conditional Operator</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.18. Comma Operators</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 + -