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

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

Thread Previous | Thread Next
From:
Graham Barr
Date:
August 11, 2009 16:03
Subject:
Re: [perl #68312] perlop / list assignment documentation
Message ID:
0D9F8483-C9CD-4C17-8D35-4760983A5488@pobox.com

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


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