perlmeditation ikegami <p>The symbol <c>=</c> is compiled into one of two assignment operators:</p> <ul> <li>A <em>list assignment operator</em> is used if the left-hand side (LHS) of a <c>=</c> is some kind of aggregate.</li> <li>A <em>scalar assignment operator</em> is used otherwise.</li> </ul> <p>The following are considered to be aggregates:</p> <ul> <li>Any expression in parentheses (e.g. <c>(...)</c>) <li>An array (e.g. <c>@array</c>) <li>An array slice (e.g. <c>@array[...]</c>) <li>A hash (e.g. <c>%hash</c>) <li>A hash slice (e.g. <c>@hash{...}</c>) <li>Any of the above preceded by <c>my</c>, <c>our</c> or <c>local</c> </ul> <p>There are two differences between the operators. <h3>Context of Operands</h3> <p>The two operators differ in the context in which their operands are evaluated.</p> <ul> <li>The scalar assignment evaluates both of its operands in scalar context. <li>The list assignment evaluates both of its operands in list context. </ul> <h3>Value(s) Returned</h3> <p>The two operators differ in what they return.</p> <table border=1> <tr><th colspan=2 rowspan=2>Returns</th><th colspan=2>Context in which Assignment Operator is Evaluated</th></tr> <tr><th>scalar</th><th>list</th></tr> <tr><th rowspan=2>Operator</th><th>scalar assignment</th><td>The LHS as an lvalue</td><td>The LHS as an lvalue</td></tr> <tr><th>list assignment</th><td>The number of scalars returned by the RHS</td><td>The scalars returned by the LHS as lvalues</td></tr> </table> <p>Note that the right-hand side is used for the list assignment in scalar context.</p> <h2>Examples</h2> <table border=1> <tr><th colspan=2 rowspan=2>Examples</th><th rowspan=2></th><th colspan=2>Context in which Assignment Operator is Evaluated</th></tr> <tr><th>scalar</th><th>list</th></tr> <tr><th rowspan=2>Operator</th><th>scalar assignment</th><td valign="top"><c> # @array evaluated in scalar context. my \$count = @array; </c></td><td valign="top"><c> # The s/// operates on \$copy. (my \$copy = \$str) =~ s/\\/\\\\/g; </c></td><td valign="top"><c> # Prints \$x. print(\$x = \$y); </c></td></tr> <tr><th>list assignment</th><td valign="top"><c> # @array evaluated in list context. my @copy = @array; </c><c> # @array evaluated in list context. my (\$first) = @array; </c></td><td valign="top"><c> # Only dies if f() returns an empty list. # This does not die if f() returns a # false scalar like zero or undef. my (\$x) = f() or die; </c><c> my \$count = () = f(); </c></td><td valign="top"><c> # Prints @x. print(@x = @y); </c></td></tr> </table> <p>Related topics: <ul> <li>[id://527973]. </ul> <p><b>Update</b>: Added examples. <br><b>Update</b>: Incorporated JavaFan's additions. <br><b>Update</b>: Removed list slices and mentioned <c>state</c>. <br><b>Update</b>: One of the examples in the scalar context column did not depend on context. It has been moved to its own column. Also, added short explanations of examples. <br><b>Update</b>: Reworded to not say there are only two assignment operators, because <c>+=</c> and such are also assignment operators. <br><b>Update</b>: Reworded to include new fangled <c>[]->@*</c> to the list of aggregates. </p>