in reply to Some Insights from a Traveler Between Languages

Your analysis of the third case is wrong. What do you expect from $x = (10, 20, 30)? If you expect 3, you're wrong -- you get 30. In this case, (10, 20, 30) is not even a list. The parentheses are used to change precendence here. Inside the parentheses are three expressions separated by the comma operator which, in scalar context (which this is), evaluates the left-hand operand and returns the right-hand operand. If we didn't have parentheses, we'd set $x to 10, and the values 20 and 30 would be in void context.

As for the fourth case, I can think of cases where I want to initialize an array to holding just one element, an array ref. More often than not, I'd expect I'd have that array ref be empty, but it's really a matter of what I'm doing.


Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Some Insights from a Traveler Between Languages
by skyknight (Hermit) on Apr 23, 2005 at 20:53 UTC
    Yeah, I totally dropped the ball on the third example. It was careless of me to use the values (1, 2, 3) as that was a mine field of coincidence and I paid the price in looking the fool. As for the fourth case, though, I really can't imagine why we would want such a thing. Surely it's not a terrible burden to have to put parentheses around the stuff you are assigning to the array.
      Surely it's not a terrible burden to have to put parentheses around the stuff you are assigning to the array.

      Then do.

      I don't because it's unnecessary. Except for slicing the values returned from a function and creating a one-element lvalue list, I cannot think of a case where parentheses create lists. (I can also argue that in the former case, they don't either; they just mark a context.)

      Put another way, if there are no precedence issues, why do you need to group a one-element list explicitly?

        I think that's skirting my main issue, though. Namely, I find it troublesome that it makes such a simple mistake so eminently possible. The fact that you have two ways of saying something and one of those two ways is extremely close to a totally different semantic meaning is just asking for trouble.

        Does the parenthesis in (EXPR) x EXPR create a list (in list context)?

        ihb

        See perltoc if you don't know which perldoc to read!