Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Why does assignment change the result?

by blazar (Canon)
on May 23, 2007 at 21:58 UTC ( [id://617123]=note: print w/replies, xml ) Need Help??


in reply to Re: Why does assignment change the result?
in thread Why does assignment change the result?

A list in scalar context is its rightmost element, an array in scalar context is its size.

You're right of course. But then you got me thinking... why does the ()= trick work? It is supposed to put its rhs in list context and then is generally put back in scalar context to get the length of a generated list... Is it special cased?

print scalar(()=eval { wantarray ? qw/foo bar baz/ : 1 });

(This prints 3 of course.)

Replies are listed 'Best First'.
Re^3: Why does assignment change the result?
by duff (Parson) on May 23, 2007 at 22:10 UTC

    I'm fairly sure it's not special cased. It's essentially what you said. (Look at the associativity see how the context propagates and look at the precedence to see who wins :).

      Yes, but then in the example above, why isn't the rightmost element of the list returned instead of its length? There's no array involved.

Re^3: Why does assignment change the result?
by Sixtease (Friar) on May 23, 2007 at 22:14 UTC
    I'm baffled by this too. How can it print 3 when there never is an array? Or is there? (I only see an empty list assigned to, which is not an array, right?)

      The "lists always return the last item and only arrays return the length" is a popular but very misleading meme.

      An array in scalar context returns the number of elements. An array slice in a scalar context returns the last value of the list that would be returned in a list context. A "literal list" (a list of expressions enclosed in parens and separated by commas) in a scalar context evaluates the last expression in a scalar context and returns the resulting value. Note that this may not be the same as the last value that would be returned if this "list" were evaluated in a list context. A list assignment in a scalar context returns the number of values on the right-hand side of the list assignment.

      The term "list" with regard to Perl is not a precise term meaning only one thing. There are lots of kinds of lists in Perl (and an array is a kind of list, especially if you define an array slice as a "list"). Each different type of "list" in Perl returns a different thing in a scalar context, usually something rather appropriate1.

      There are lots of discussions of this around the Monastery so I'm bit surprised that so many participants of this thread remember so little from them. :)

      - tye        

      1 Note that what an array slice returns (or was it hash slices or both?) in a scalar context was never designed nor documented and was only implemented after a patch I wrote and forgot about years ago. It also happens to be one of the rare cases when Perl "makes a list (of scalar values on the stack) despite a scalar context and then throws all but one of them away", which Perl usually avoids.

      Update: Had I better understood Perl's list/scalar contexts at the time I wrote the patch, I probably would have at least wanted to make the patch instead pass the scalar context inward to the array slice indices expression (or to the hash slice keys expression) so that only one index (key) was calculated and then used to determine which single element to return.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://617123]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found