|more useful options|
Re^5: Confused as to why the "casting context" is mis-behaving (trap)by tye (Cardinal)
|on Oct 21, 2010 at 14:16 UTC||Need Help??|
The example from perltrap is unfortunately ambiguous.
Not even the fact that they have effectively the same assignment (from a list to a scalar) and claim that the result will be the length of the list?
No, it doesn't get the length of the list (literal). It gets the length of the array, which is the last expression in the list literal. The last word of the example is worse than ambiguous, it is misleading. One can argue that it isn't technically incorrect because $x does get assigned the length of the list of scalar values currently in the array, @y. But it certainly looks incorrect if you view it through the broken lens of the false dichotomy of "array vs list".
I.E. that note in perltrap is wrong, at least for those versions of perl that I have access to.
If you run the provided code in your copy of Perl 5 the output is not the documented "x = 3"? If so, then you should file a bug report.
The example would not be ambiguous if slightly modified:
This illustrates why I said "be careful how you define 'item'". A list literal can be said to return its last "item" in scalar context. But it is much more accurate to say that a list literal in a scalar context calls the last expression in scalar context and returns (just) that result. A list literal is a syntax construct so "last item" means the last expression, whatever appears after the last literal comma in that part of the source code.
A list literal can return a list of scalar values. But the last value of that list of values is not always the same as the "last item" that the list literal returns in scalar context. This further proves that a list literal is not the same as a list of scalar values.
A slice in scalar context actually does return the last value. A list literal returns the scalar value of the last expression. The perltrap excerpt illustrates the difference.