http://www.perlmonks.org?node_id=720267


in reply to Re^3: If you believe in Lists in Scalar Context, Clap your Hands
in thread If you believe in Lists in Scalar Context, Clap your Hands

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:

my $x = 1, 2, 3;

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?

my @y = 1 .. 8; my $x = @y, 2, 3; print $x;

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.