Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by oshalla (Deacon)
on Oct 23, 2008 at 23:09 UTC ( #719191=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

Let's suppose for a moment that Perl, as a language, is a collection of concepts, without worrying too much about how a program written using that language is executed.

It seems to me that in this language the list (or LIST) is an important element. It appears that lists may appear as arguments in expressions and may be the result of expressions.

I can see that it is possible to take the view that lists (in this abstract sense) can only exist in List Context. Trivially then there can never be a List in Scalar Context.

This view requires a Scalar Context variant for every instance of something that in List Context would return a list. The Scalar interpretation of ',' is well known. Similar definitions are required for slice operations, and for anything else that can return a list in the appropriate context.

It's true that in the limit, Perl being what it is, the programmer should never assume any particular behaviour in a Scalar Context based on knowledge of the List Context behaviour.

Bearing that in mind, there are two common Scalar Context results where a list is returned in List Context:

  1. the length of the list that would have been returned in List Context.
  2. the last item of the list that would have been returned in List Context.

These are, of course, not the only results -- just common ones.

As common as they are, my feeling is that these behaviours could usefully be given a name. Then using that name the programmer might reduce the intellectual load by categorising some things as Type-1 list operations and others as Type-2.

It would be nice to have a more memorable or meaningful names for these... perhaps something that relates to concepts already understood ?

I have a couple of fairly obvious suggestions. But perhaps I have uttered enough heresy for today.


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://719191]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-08-23 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (176 votes), past polls