<?xml version="1.0" encoding="windows-1252"?>
<node id="790129" title="Mini-Tutorial: Scalar vs List Assignment Operator" created="2009-08-20 12:05:53" updated="2009-08-20 12:05:53">
<type id="120">
perlmeditation</type>
<author id="381608">
ikegami</author>
<data>
<field name="doctext">
&lt;p&gt;There are two assignment operators, the list assignment operator and the scalar assignment operator. If the LHS of an assignment is some kind of aggregate, the list assignment is used. Otherwise, the scalar assignment is used. The following are considered to be aggregates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;c&gt;(...)&lt;/c&gt; (any expression in parens)
&lt;li&gt;&lt;c&gt;@array&lt;/c&gt; and &lt;c&gt;@array[...]&lt;/c&gt;
&lt;li&gt;&lt;c&gt;%hash&lt;/c&gt; and &lt;c&gt;@hash{...}&lt;/c&gt;
&lt;li&gt;&lt;c&gt;my (...)&lt;/c&gt;, &lt;c&gt;our (...)&lt;/c&gt; and &lt;c&gt;local (...)&lt;/c&gt; (&lt;c&gt;state (...)&lt;/c&gt; throws an error.)
&lt;/ul&gt;

&lt;p&gt;There are two differences between the operators. The first is the context in which operands are evaluated:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The scalar assignment evaluates both of its operands in scalar context.
&lt;li&gt;The list assignment evaluates both of its operands in list context.
&lt;/ul&gt;

&lt;p&gt;The second difference is in what they return:&lt;/p&gt;

&lt;table border=1&gt;
&lt;tr&gt;&lt;th colspan=2 rowspan=2&gt;Returns&lt;/th&gt;&lt;th colspan=2&gt;Context in which Assignment Operator is Evaluated&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;scalar&lt;/th&gt;&lt;th&gt;list&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th rowspan=2&gt;Operator&lt;/th&gt;&lt;th&gt;scalar assignment&lt;/th&gt;&lt;td&gt;The LHS as lvalue&lt;/td&gt;&lt;td&gt;The LHS as lvalue&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;list assignment&lt;/th&gt;&lt;td&gt;The number of elements returned by RHS&lt;/td&gt;&lt;td&gt;The elements returned by LHS as lvalues&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;Note that the right-hand side is used for the list assignment in scalar context.&lt;/p&gt;

&lt;p&gt;Finally, here are some examples:&lt;/p&gt;

&lt;table border=1&gt;
&lt;tr&gt;&lt;th colspan=2 rowspan=2&gt;Examples&lt;/th&gt;&lt;th colspan=3&gt;Context in which Assignment Operator is Evaluated&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;&amp;#91;any]&lt;/th&gt;&lt;th&gt;scalar&lt;/th&gt;&lt;th&gt;list&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th rowspan=2&gt;Operator&lt;/th&gt;&lt;th&gt;scalar assignment&lt;/th&gt;&lt;td valign="top"&gt;&lt;c&gt;
# Array evaluated in scalar context.
my $count = @array;
&lt;/c&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;c&gt;
# The s/// operates on $copy.
(my $copy = $str) =~ s/\\/\\\\/g;
&lt;/c&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;c&gt;
# Prints $x.
print($x = $y);
&lt;/c&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;list assignment&lt;/th&gt;&lt;td valign="top"&gt;&lt;c&gt;
# Array evaluated in list context.
my @copy = @array;
&lt;/c&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;c&gt;
# 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;
&lt;/c&gt;&lt;c&gt;
my $count = () = f();
&lt;/c&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;c&gt;
# Prints @x.
print(@x = @y);
&lt;/c&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;Related topics:

&lt;ul&gt;
&lt;li&gt;[id://527973].
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;Update&lt;/b&gt;: Added examples.
&lt;br&gt;&lt;b&gt;Update&lt;/b&gt;: Incorporated JavaFan's additions.
&lt;br&gt;&lt;b&gt;Update&lt;/b&gt;: Removed list slices and mentioned &lt;c&gt;state&lt;/c&gt;.
&lt;br&gt;&lt;b&gt;Update&lt;/b&gt;: 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.
&lt;/p&gt;
</field>
</data>
</node>
