Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

by gone2015 (Deacon)
on Oct 24, 2008 at 12:30 UTC ( #719332=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

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://719332]
and a moth chases the moon...

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

    Results (121 votes). Check out past polls.