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

Re: "Secret" operators

Thread Previous | Thread Next
Quantum Mechanic
February 1, 2005 11:55
Re: "Secret" operators
Message ID:

--- Bernie Cosell <> wrote:

> On 1 Feb 2005 at 12:36, Ronald J Kimball wrote:
> > ... A list assignment in scalar context
> > returns the number of elements on the right-hand
> side of the assignment.
> Which is an odd inconsistency, because in list
> context a list assignment 
> returns the left-hand-side-list, so you might guess
> that in scalar 
> context it'd return the number of elements in the
> lhs list...

It seems there are multiple contexts flying around.
I'm no expert, but it seems to develop this way:

$foo = $bar = (9,8,7);

    RHS list
    $bar scalar
    $bar <- 7 (last element of list)
    $foo <- 7

$foo = ($bar) = (9,8,7);

    RHS list
    ($bar) array
    $bar <- 7 (rest discarded)
    $foo <- 3, size of RHS of ($bar) assignment


$foo = () = (9,8,7)

    RHS list
    () array
    no assignment targets, but...
    $foo <- 3, size of RHS of () assignment

My assumption is that =()= creates an intermediate
assignment, which subtley shifts the context so that
$foo gets the count of (9,8,7) in the last case,
instead of the last element of (9,8,7) as in the first

=()= forks the assigment flow. The elements of the
right operand are assigned (if possible) to the
contained array, as in

    $foo = ($bar) = (9,8,7);
    $foo = @bar   = (9,8,7);

The left operand of =()= is assigned the element count
of the other assignment.

Someone familiar with the core will have to clue me in
on the accuracy of my theory.


Quantum Mechanics: The dreams stuff is made of

Do you Yahoo!? 
The all-new My Yahoo! - Get yours free! 

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About