|Perl: the Markov chain saw|
Re^5: Confused as to why the "casting context" is mis-behavingby ig (Vicar)
|on Oct 21, 2010 at 17:54 UTC||Need Help??|
But I do find it interesting, as I said.
In fact, that example led me to a much more correct understanding of what is happening. I wasn't aware of it and would never have looked for such insight into how literal lists behave in perltrap, so thank you for pointing it out.
I agree that what you found in perltrap should be presented elsewhere. I had said Context but on further consideration I think List value constructors would be a better place. There is already an example with a scalar variable as the last "value" (using the terminology in List value constructors, though I wonder if that is the best terminology) in the literal list. I think the example from perltrap would be a fine addition to that section.
I do think there are faults in the example in perltrap. I get the same result described with the version of perl5 I have tested with. I don't have any perl4 handy to test that prior behavior with.
The description in perltrap is inconsistent with perl 5.10.0 that I have recently been testing with. The description says:
The comma operator in a scalar context is now guaranteed to give scalar context to its arguments.
So, the example says the comma operator is guaranteed to give scalar context to its arguments when evaluated in scalar context, but in perl 5.10.0 I see void context in some cases.
This raises yet another issue: what does the comma operator (or any operator) do in void context?
perlop says the comma operator is left associative. I take this to mean that ( 1, 2, 3 ) is the same as ( ( 1, 2 ), 3 ) rather than ( 1, ( 2, 3 ) ). If I am correct, one description of the comma operator that is consistent with my observations would be:
But, I haven't looked at the internals of perl, and this may be more of the nonsense that so frustrates tye.
While tye said the comment at the end of the example you quote is misleading, I would go further and say it is wrong. It should say (IMHO) "Knows scalar uses length of array", as tye said. The ambiguity is, I think, in which "list" is referred to in the last comment in the example. The comment is correct if one considers it to refer to the list of values in the array @y, but incorrect if one considers it to refer to the literal list ending with @y.
More than reporting bugs, most people will continue to produce nonsense descriptions and explanations if this isn't better documented. This will lead to many surprises, which Perl/perl is supposed to avoid. In this case, I think better documentation is the most pragmatic means to reducing the element of surprise.