On Mar 16, 3:39*pm, Mark <(EMail Removed)> wrote:
> Why does the introduction of the assignment to @b affect the value of
> $cnt?
>
> use strict;
> use warnings;
>
> my $cnt;
> my($x,$y,$z);
> my(@ary) = qw(a b c d e);
>
> $cnt = ( ($x,$y,$z) = @ary );
> print "cnt=$cnt\n"; # produces 5
>
> my @b;
> $cnt = ( @b = ($x,$y,$z) = @ary );
> print "cnt=$cnt\n"; # produces 3
See: perldoc perldata and its explanations
of lists and list vs. scalar context
Specifically, from that doc:
List assignment in scalar context returns the
number of elements produced by the expression
on the right side of the assignment:
$x =($foo,$bar) = (3,2,1));# set $x to 3, not 2
...
Similarly, in your first case, that evaluates to 5.
The eventual array assignment to @b in the second
case is also evaluated in scalar contest. The first
three elements in @ary are assigned to ($x,$x,$z)
and the additional elements in @ary are discarded.
@b is then assigned ($x,$y,$z) and then is evaluated
in scalar context to yield 3.

Charles DeRykus
