Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by gone2015 (Deacon)
on Oct 23, 2008 at 23:09 UTC ( [id://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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found