Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: list assignment to list in scalar context

by Anonymous Monk
on Nov 09, 2012 at 15:45 UTC ( #1003167=note: print w/replies, xml ) Need Help??

in reply to list assignment to list in scalar context

Ok, here goes.


has no list in it. The confusion is that we (English speakers) "list" things by separating them using commas. From Perl's perspective, there is nothing in that statement to provide a list context. "scalar" can take any expression, and just sees comma separators, no matter how many sets of parentheses you surround it with.


on the other hand, has an assignment that forces a list context, so the answer becomes "3".

if( ($k, $v) = ( 1, 0 ) )

works the same way. The left side of the assignment is now a list, because of the equals sign. The overall context of any boolean test is scalar, so that an "if list" always provides the element count.

Replies are listed 'Best First'.
Re^2: list assignment to list in scalar context
by Anonymous Monk on Nov 15, 2012 at 08:50 UTC

    @TGI: You have said that there is no list at all in:

    if( (5,3,0) )
    I tested below. You are right.
    if( (5,3,0) ) is equal to if( 5,3,0 )
    both evaluate to FALSE. I need to understand the definition of list furthur.

    @tye and Anonymous Monk : Your comments make sense.

    From your patient answers, I think I can draw the conclusion: The reason that list assignment in scalar context will return the number of elements on right side list is for convenience of usage.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003167]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-06-22 06:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.