Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^4: If you believe in Lists in Scalar Context, Clap your Hands

by gone2015 (Deacon)
on Oct 24, 2008 at 00:29 UTC ( [id://719210]=note: print w/replies, xml ) Need Help??


in reply to Re^3: If you believe in Lists in Scalar Context, Clap your Hands
in thread If you believe in Lists in Scalar Context, Clap your Hands

Perhaps I don't fully understand right associativity.

Consider:

$r = () = 0..11 ; $s = ($x, $y) = 0..11 ;
which I thought meant that the 0..11 would be evaluated in List Context, and then assigned to the lvalues in lists () and ($x, $y) -- the rightmost assignment first.  Then the result of that assignment would be assigned to <c>$r and $s this being a scalar assignment. The actual result is that both $r and $s are set to 12. This seems to imply that the first, list, assignment assigns the entirity of (0, 1, ...11) to the intermediate list, irrespective of the number of lvalues it contains. Then the length of that list is assigned to $r. This last step is consistent with the well known Array in Scalar Context... though () doesn't look like an array to me... but I'm willing to believe.

Of course:

$s = ($x, $y) ;
assigns $y to $s. But I'm willing to accept that this is quite different from the above. I just don't yet see how.

As you say, if we then look at:

@r = () = 0..11 ; @s = ($x, $y) = 0..11 ;
we find that @r is set to the empty list and @s to the two element list (0, 1). I confess this is more what I expected. The result of the second assignment depends on the result of the first (rightmost).

Is it just me ? Is this in fact consistent at some deeper level I don't yet understand ?

Replies are listed 'Best First'.
Re^5: If you believe in Lists in Scalar Context, Clap your Hands
by ysth (Canon) on Oct 28, 2008 at 05:20 UTC
    I think the inner consistency you are missing is that operands aren't only values, they can be operations as well. The left = (the scalar assignment) has as its right operand not the "result of [the list] assignment", but rather the list assigment operation. And that operation, by virtue of being the right side of a scalar assignment, gets scalar context, and the "result" of a list assignment in scalar context is the number of elements on the right of the assignment. Does that help at all?
Re^5: If you believe in Lists in Scalar Context, Clap your Hands
by JavaFan (Canon) on Oct 24, 2008 at 00:41 UTC
    The difference between
    $s1 = ($x, $y) = 0..11;
    and
    $s2 = ($x, $y)
    is that $s1 gets assigned the result of the list assignment operator, and $s2 gets assigned the result of the scalar comma operator. In neither case there's a list assigned to any of the $s? variables; in both cases the result of operators are assigned.

    But as long as you keep believing in the existence of lists in scalar context, you'll keep being confused.

      But as long as you keep believing in the existence of lists in scalar context, you'll keep being confused.

      Up to a point.

      $s1 = ($x, $y) = 0..11;
      is explained by reference to the definition, which refers to the Scalar Context effect on the assignment, independent of the list being assigned to.

      This piece of magic cannot be understood either as a List in Scalar Context or as a Scalar Context ',' operation.

      Apples and pears wise, what we have here is a banana.

        And they're all bananas. Which is a much simpler way to understand it all. :)

        - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-24 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found