develooper Front page | perl.perl5.porters | Postings from August 2009

Re: [perl #68312] perlop / list assignment documentation

Thread Previous | Thread Next
From:
David Nicol
Date:
August 11, 2009 13:41
Subject:
Re: [perl #68312] perlop / list assignment documentation
Message ID:
934f64a20908111340n47399597t343335e74299a986@mail.gmail.com
On Tue, Aug 11, 2009 at 2:31 PM, Eirik Berg Hanssen<Eirik-
>>      As an r-value, the list assignment operator passes its own
>> r-value through.
>
>  What is "its own r-value"?  If you mean to say the "elements
> produced by the expression on the right hand side of the assignment",
> it is incorrect.
>
>  (And if you meant something else, I found it less clear than what
> the patch had.)

Yes, that's what I meant.  Thanks for straightening out my misconception.


>
> use 5.010;
> use strict;
> use warnings;
> my @x = my ($x, undef, $y) = 1..10;
> say $_||'undef' for @x;
> __END__
> 1
> 2
> 3
>
>  3 elements, not 10.  Specifically, the three elements assigned to.

wow.

/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.



>
>  Aside:
>
>  And none of them undef either.  In fact, this is one ... rather
> _special_ ... corner case:
>
> use 5.010;
> use strict;
> use warnings;
> for my $u (undef) {
>  my @x = ($u) = 1..10;
>  say $_||'undef' for @x, $u;
> }
> __END__
> 1
> undef
>
>  The assignment to ($u) evaluates to a list of one element, the value
> of which differs from the value of $u, which remains the same both
> before and after the assignment.  Ah, the magic of undef. :)

Is that not a bug, that an assignment to a constant ($u is an alias to
undef) did not cause an exception?  It looks like undef is special
that way.

/tmp: $ perl -le ' $count = undef = 3; print $count'
Modification of a read-only value attempted at -e line 1.
/tmp: $ perl -le ' ($count) = (undef) = (3); print $count'
3
/tmp: $ perl -le ' ($count) = (22) = (3); print $count'
Can't modify constant item in list assignment at -e line 1, near ");"
Execution of -e aborted due to compilation errors.
/tmp: $

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About