Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by oshalla (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.


Comment on Re^4: If you believe in Lists in Scalar Context, Clap your Hands
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-08-30 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls