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


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

What I think JavaFan is getting at when he says "lists in scalar context" is a damaging meme is that there is no such thing, and he feels that this conception leads to serious errors of understanding.

See perlop for why there is no such thing:

A named array in scalar context is quite different from what would at first glance appear to be a list in scalar context. You can't get a list like (1,2,3) into being in scalar context, because the compiler knows the context at compile time. It would generate the scalar comma operator there, not the list construction version of the comma. That means it was never a list to start with.

There's a bit more in the 'Comma Operator' section of perlop.

So, when you have
    $r = (1, 5, 10)
perl sees something like
    $r = 1 return_right_side 5 return_right_side 10;
and
    @r = (1, 5, 10)
becomes something like
    @r = list_constructor( 1 list_separator 5 list_separator 10 );


TGI says moo

Replies are listed 'Best First'.
Re^4: If you believe in Lists in Scalar Context, Clap your Hands
by gone2015 (Deacon) on Oct 24, 2008 at 12:30 UTC
    See perlop for why there is no such thing:
    A named array in scalar context is quite different from what would at first glance appear to be a list in scalar context. You can't get a list like (1,2,3) into being in scalar context, because the compiler knows the context at compile time. It would generate the scalar comma operator there, not the list construction version of the comma. That means it was never a list to start with.

    The thing is, this is only a partial explanation.

    First, the issue is not entirely resolvable at compile-time. Take a subroutine that ends: return (1, 2, 3) ; The compiler must either have a Context sensitive variant of the comma operator, or compile in the list version followed by code to tidy up afterwards to match the calling context.

    Second, literal lists are not the only lists. The most general example of that is the slice. I accept that these can be explained by reference to Scalar Context behaviour of the slice operators.

    But all of this can also be understood in terms of List in Scalar Context semantics -- a form of "default coercion", if you will. Without recourse to "it cannot be a list if it's not in List Context" mental gymnastics.

    And, for completeness, this is only effective with the caveat that to assume that this "default coercion" is universal will, sure as eggs is eggs, lead to tears before bedtime. But then, that's true of many defaults.