On Sat, 5 Oct 2002, Chip Salzenberg wrote: : According to Larry Wall: : > I suppose we could make comma merely puke in scalar context rather : > than DWIM, at least optionally. : : I rather like Perl 5's scalar comma operator.
Most of the uses of which are actually in void context, where it doesn't matter if we technically build a list and throw it away, or throw away all but the last value, then throw that one away too. Which is why it got decided the way it was. If you really want the last value, you can always use (LIST)[-1] which could be construed as better documentation. Admittedly it's not quite the same, since LIST is evaluated in list context. Anyway, if we did outlaw comma in scalar context, it would not apply to void context, or we couldn't write: loop ($a=0, $b=1; $c; $a++, $b++) { ... } : > : $a = (); # $a is a list reference with 0 elements : > : $a = (10); # $a is the scalar 10 : > : $a = (10, 20); # $a is a list reference with 2 elements : > : > I don't think that's an important inconsistency. : : What if $a and $b are both array refs, and maintenance programmer : changes: : for $a, $b { print } : to: : for $a { print } : Will that end up iterating across @$a? The equivalent of that gotcha : was forever bugging me in Python (gack). No, in list context reference scalars must still be explicitly dereferenced. The $ on the front still means something occasionally. It's only in scalar contexts that $ can be autodereffed as an array now, so that someone can say push $a, @list; But if you say push $a, $a; you get the ref in $a pushed onto @$a, as it currently stands. Larry