develooper Front page | perl.fwp | Postings from February 2005

Re: "Secret" operators

Thread Previous | Thread Next
From:
Ronald J Kimball
Date:
February 1, 2005 09:37
Subject:
Re: "Secret" operators
Message ID:
20050201173646.GB31094@penkwe.pair.com
On Tue, Feb 01, 2005 at 11:15:46AM -0600, Andy_Bach@wiwb.uscourts.gov wrote:
> Can I get that just a little slower?
> 
>  $b = () = /u/g;
> 
> is the same as:
> 
>    @a = /u/g;
>    $b = @a;
> 
> I understand what happens, but it appears to be assigning to an empty list 
> - is that filling up the list, so to speak?  Or is it just that it makes 
> the 'result' of /u/g assign in array/list context and then that, assigned 
> in scalar context to $b gives the list/array count. 

It is assigning to an empty list.  A list assignment in scalar context
returns the number of elements on the right-hand side of the assignment.


You can assign two elements to a two-element list:

($foo, $bar) = (1, 2);

You can assign two elements to a one-element list:

($foo) = (1, 2);

You can even assign two elements to an empty list:

() = (1, 2);


In each case, any extra elements are simply discarded, but the result of
the assignment in scalar context is always the number of elements on the
right-hand side, even if some aren't actually assigned to variables.


Ronald

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