http://www.perlmonks.org?node_id=421051


in reply to Re^2: List assignment in scalar context
in thread List assignment in scalar context

Oh come off it.

sub list { return 1..5 } my ($a,$b,$c); my $n1 = ($a,$b,$c) = list(); my $n2 = my @x = ($a,$b,$c) = list(); print "$n1 ... $n2\n"; ---- 5 ... 3

When you have scalar = list = list, you will always get the count of items in the rightmost list in scalar. In your $n2 example, your array is getting three items from the list. It isn't a scalar, so it isn't getting the count. What else would be expected? The behavior that merlyn is referring to is the reason why you don't see the complete count in the scalar.

Replies are listed 'Best First'.
Re^4: List assignment in scalar context
by dragonchild (Archbishop) on Jan 10, 2005 at 19:25 UTC
    So, based on what you're saying, $n2 should equal 5. Since @x isn't a scalar, it shouldn't affect the count of $n2 because I have scalar = list = list = list ... so, why doesn't that work?

    Not to mention, where is it documented that When you have scalar = list = list, you will always get the count of items in the rightmost list in scalar. ?? I don't remember ever reading that ...

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      = is right associative. Thus you have list = list (rightmost) before you get scalar = list = list. The list in question has 3 elements. Of course in this case, I think the behavior of an array in scalar context has more to do with the overall result than anything else. Note that if you replace the array declaration with any list of the form ($a,$b,...), you still get the count in $n2.

      perldoc perldata, look for "List assignment in scalar context". Note that the extra parentheses around the rightmost assignment in the example are superfluous since they don't affect the order of operations.

      Also, to those downvoting my nodes in this thread: if the reason that you're downvoting my nodes is due to the lack of technical merit of my posts, feel free to /msg me or reply.

      Since @x isn't a scalar, it shouldn't affect the count of $n2 because I have scalar = list = list = list

      No, you don't. You have scalar = array = list = list, which, as far as the scalar is concerned, reduces to scalar = array, after the array is assigned its value. An array is not a list. The Camel is quite clear on that point. It's also clear on what an array returns in scalar context. No surprises there.