Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

List context, expression precedence, and ()

by pmas (Hermit)
on Jul 21, 2001 at 20:58 UTC ( [id://98673]=perlmeditation: print w/replies, xml ) Need Help??

This node started as answer to difference between ($scalar) and $scalar, but I realized I do not offer answers, so I better put it here - as my first meditation...

After programming many years in languages where () were used only to define expression precedence and to pack together parameters of the function, I started to learn "programming perl", where () are used also to show list context. From being used to spread () freely around and inside expressions, I became sometimes kind of paranoid and saving some calculation steps in temporary scalar variables just to make sure they are scalars (it laso helps for debugging to print them...) IMHO () and comma opperator might be rather frustrating feature for programmers experienced in other languages - at least for me. I know it is not going to happen, but for Perl6 I would like to have () for expression precedence as in most other languages, and for simple showing list context some other operator, like [ ] or something.

Does anybody here knows why this confusing decision was made? Does it have some deep reason, what I am missing now? I can understand that list of parameters of the function I want to wrap into () as in other languages, but free-standing list - it feels for me little different.

I have nothing for perl only praise, I like Perl a lot, I coded in many other languages and each of them has some annoying features. Perl is clearly balanced into positive side, but still...What is your opinion/feelings (except stuff already written in other meditations to this theme..;) )?

pmas
To make errors is human. But to make million errors per second, you need a computer.

  • Comment on List context, expression precedence, and ()

Replies are listed 'Best First'.
Re: List context, expression precedence, and ()
by HyperZonk (Friar) on Jul 21, 2001 at 21:36 UTC
    As you get used to Perl programming, I think you will find that (lists) are your friends. Experience will undoubtedly teach you when parentheses can be used only for precedence.

    Also, I think that you will find that the specific uses for [brackets] to construct lists are also worth the small inconvenience of not being able to use parentheses without considering whether or not you are emitting a list. You see, square brackets can be used to generate an anonymous list reference, so you can generate a list of lists, or a list within a hash. Very powerful data structures can easily be built in Perl with these constructs. Detailed information about these constructions can be found in the perlref documentation.

    Attacking lists may be seen by some as tilting at a sacred part of Perl, so some people might be tempted to downvote your meditation. I would suggest otherwise; your question is valid coming from a new Perl programmer, and should be answered. But I do encourage you to explore the power of lists in Perl, and the ease of creating complex data structures with Perl's current syntax. I think you will end up loving (lists) and their syntax as much as most of the rest of us.

    Cheers
      I do like lists, it is so much more flexible for passing parameters, easy to build optional parameters, etc. I just need always be concerned about scalar/list context in expressions, that's all.

      I found japhy's article about scalar/list context, and it was real eye-opener, when I learned that

      sub A { return ('a', 'b', 'c'); } $a = A(); # $a gets value 'c' ($b) = A(); # $b gets value 'a'
      As japhy explains:
      The A() function, in scalar context, has the comma operator act on the comma separated series of values, and so $a is set to 'c' which makes sense. $b, however, gets the value 'a', because it invokes list context on the function, which returns a list.

      japhy: ++ for you!

      pmas
      To make errors is human. But to make million errors per second, you need a computer.

Re (tilly) 1: List context, expression precedence, and ()
by tilly (Archbishop) on Jul 21, 2001 at 22:18 UTC
    If you think that parentheses should only indicate precedence, then you have clearly never experienced any of the Lisp family of languages.

    For me after I learned what context was I made the decision that in my code the callee was responsible for working in either list or scalar context, and then it was the caller's job to not play games with it. Having somewhere to place that responsibility has made consistent handling of it work out quite well.

    But yes, this feature is one that Perl programmers far too often gloss over, and it is so key to the language that they are really missing something if they don't understand it.

(tye)Re: List context, expression precedence, and ()
by tye (Sage) on Jul 22, 2001 at 03:42 UTC

    Actually, () only very rarely denotes list context. I've been trying to figure out all of the possible ways to use () to create a list context and I have yet to come up with more than one: List assignment. (Perhaps I'm overlooking some?)

    That is, taking a scalar assignment and adding parens around the left-hand side switches the right-hand side from scalar to list context. I can't come up with any other cases where parens change the context.

    So the only change you'd need to require for Perl6 is a new syntax for list assignment. Since     [$a,$b]= @list; isn't already taken, that would work.

    But, it is also true that you've misinterpretted the problem and made it to be much bigger than it really is. So perhaps the change isn't as necessary.

    Actually, if we did make it so that (...)= no longer denoted a list assignment and so left the right-hand side in a scalar context, then we'd have new surprises because:      my( $a, $b )= ( 4, 5 ); would be interpretted as:      my $a; my $b= 5;

    So I don't think you'll have much luck convincing Larry and Dominus to make that change.

    But I have an idea for another change that might have a chance of getting accepted. How about an compile-time warning for:      ( $scalar )= EXPR that suggests that you either drop the parens or rewrite that as:      $scalar= ( EXPR )[0] depending on what you really meant?

    Even that might be a bit of an up-hill fight as code like:     my( $arg )= shift; works just fine since shift returns a scalar. I don't know how easy it would be to generate the warning only if the right-hand side cared about its context...

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found