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

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

Thread Previous | Thread Next
From:
Eirik Berg Hanssen
Date:
August 12, 2009 00:46
Subject:
Re: [perl #68312] perlop / list assignment documentation
Message ID:
7x8whpo4ps.fsf@blackbox.eirik.dav
Graham Barr <gbarr@pobox.com> writes:

> On Aug 11, 2009, at 3:40 PM, David Nicol wrote:

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

  The RHS value, perhaps, but not the RHS element itself.

  Just like your elegant tied() demonstration, list assignment in
lvalue list context reveals that what is returned is a copy of the RHS
element – even in the case of a constant undef on the LHS:

use 5.010;
use strict;
use warnings;
my $y = 2;
for my $u (undef) {
  my ($x1, $x2);
  for my $x (($x1, undef, $u, $x2)=($y, $y, $y, $y)) {
    $x**=2;
    $y++;
    say "x: $x; y: $y, u: " . ($u//'undef');
  }
}
__END__
x: 4; y: 3, u: undef
x: 4; y: 4, u: undef
x: 4; y: 5, u: undef
x: 4; y: 6, u: undef

  Compare:

use 5.010;
use strict;
use warnings;
my $y = 2;
for my $x (($y, $y, $y, $y)) {
  $x**=2;
  $y++;
  say "x: $x; y: $y";
}
__END__
x: 5; y: 5
x: 26; y: 26
x: 677; y: 677
x: 458330; y: 458330


  Without spending too much text on corner cases nor misleading the
reader too much, "the list of lvalues assigned to", as in the docs
both before and after the proposed patch, is about as informative as
it gets on this issue.  Or so I suspect.


Eirik
-- 
The basic facts are that the rate of decrease of the population growth
rate has been falling for decades, at an ever increasing rate.
        --jsnead@netcom.com (John R. Snead)

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