On Aug 11, 2009, at 3:40 PM, David Nicol wrote:
>
>
> /tmp: $ perl -le '@a=qw/24 57 32 hike!/; $count = () = @a; print
> $count'
> 4
> /tmp: $ perl -le '@a=qw/24 57 32 hike!/; @copy = () = @a; print @copy'
>
> /tmp: $
>
> So list assignment in scalar context returns the count of the RHS, but
> list assignment in list context returns the LHS.
unless the element in the LHS is undef, in which case it returns the
RHS value for that element, as can be seen in
$ perl -le '@a=qw/24 57 32 hike!/; @copy = ($x,undef,$y) = @a; $,=",";
print @copy'
24,57,32
I was about to say that a list assignment in a list context returns
the first N elements of the RHS, where N is the number of elements on
the LHS, but this is not true when tied variables are involved
{package Bar;
sub TIESCALAR { return bless {} }
sub STORE { return 99 }
sub FETCH { return 88 }
}
tie $x, 'Bar';
$,=",";
@copy = ($x,$y) = (1,2);
print @copy;
will output
88,2
Graham.
Thread Previous
|
Thread Next