|Perl Monk, Perl Meditation|
Re^4: If you believe in Lists in Scalar Context, Clap your Handsby chromatic (Archbishop)
|on Oct 29, 2008 at 17:43 UTC||Need Help??|
The proposed rule was:
When a LIST is evaluated in scalar context every element of the list is evaluated in scalar context and the value of the last element is the value of the LIST, with all the other values being discarded.
I said that doesn't account for precedence. Here's a very simple example:my $x = 1, 2, 3;
Many people might say that 1, 2, 3 is a list, and it's obvious that my $x = supplies scalar context. The problem (at least for the proposed rule) is that = has a higher precedence than ,, so Perl's parser turns this code into three expressions:
Because the second and third expressions have no side effects (they're just constants) and they're in void context (use warnings to see that), they get optimized away. That's why they're not present if you use B::Deparse to see what Perl sees.
(Besides that, this proves that the general rule "A list in scalar context evaluates to its final element" is wrong, too.)
How about another example?
That's why I say the proposed rule has big problems. What looks like a list to a casual reader is often not the list that Perl sees -- and all of the talk about evaluating elements in scalar context and evaluating to the final element of the list is wrong.