Perl Monk, Perl Meditation PerlMonks

### Re^2: If you believe in Lists in Scalar Context, Clap your Hands

by chromatic (Archbishop)
 on Oct 29, 2008 at 06:40 UTC ( #720165=note: print w/replies, xml ) Need Help??

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.

That explanation fails to account for operator precedence, so it's unworkable in practice.

• Comment on Re^2: If you believe in Lists in Scalar Context, Clap your Hands

Replies are listed 'Best First'.
Re^3: If you believe in Lists in Scalar Context, Clap your Hands
by ig (Vicar) on Oct 29, 2008 at 17:43 UTC
That explanation fails to account for operator precedence, so it's unworkable in practice.

I don't understand your point. Can you give an example or explain how it fails to account for operator precedence?

Note that I am talking about LISTs, not list values or all expressions that have list values in list context. I think it is necessary to distinguish between these three quite different things and that failure to do so is at the root of some of the confusion and disagreement that has been expressed.

Update: chromatic responded to AM about the same time I posted this.

Re^3: If you believe in Lists in Scalar Context, Clap your Hands
by Anonymous Monk on Oct 29, 2008 at 07:14 UTC

Would you elaborate on the affect of precedence on this please? With an example if possible.

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.

Taking into account operator precedence in your first example, the 1 is all that's in scalar context. There is nothing to present a list to the assignment operator at all, and each element item to the right of the assignment operator gets its context separately (two thirds of them getting void context).

Likewise taking into account operator precedence in your second example, only @y is in scalar context. The exception that an array in scalar context yields its length is in effect, and that's what \$x gets assigned.

No lists are presented to the operators due to comma being lower in precedence than assignment. I don't see how one's thinking about lists even becomes involved with the thinking about the assignment unless someone doesn't know that the comma operator is lower precedence than assignment.

If someone doesn't understand operator precedence, they have bigger issues than just a mental model of lists.

BTW, I've never programmed more than a couple of dozen lines of Python, total. I also think candy-flavored unicorns sound delightful, if a bit dangerous to taste.

Now, if we were dealing with a concept of lazy vs. eager assignment operators or specific atom and list assignment operators (which would need different names and not both be simply '=') instead of context being determined by the left operand of the infix operator, then that'd be another story. That'd be a different language, too, though.

I don't mean to be argumentative, but I am as yet unconvinced that the rules I gave are unworkable in practice.

To begin on a positive/agreeable note... you say:

What looks like a list to a casual reader is often not the list that Perl sees

I agree completely. And this problem is exacerbated by the lack of a clear definition of what is a LIST in the Perl documentation.

Many people might say that 1, 2, 3 is a list...

The rule I gave is a rule for evaluating a LIST in scalar context. It is not a rule for determining what part of a statement is a LIST or what the elements of the LIST are.

Other rules are required to determine what is a LIST and your example illustrates some of the complexity of doing so. It does not convince me that the rule I gave is unworkable in practice.

While some statements are difficult to parse, others are simple. The use of optional parentheses can simplify the task which otherwise, as you point out, requires an intimate knowledge of precedence and other rules.

The fact that some statements are difficult to parse does not mean that there is no such thing as a LIST in scalar context.

...they get optimized away...

The rule I gave was a rule describing the semantics of Perl, not the algorithms of perl. That perl can and does use optimized algorithms for executing Perl programs is a good thing as long as it remains consistent with the syntax and semantics of Perl. I don't agree that an optimization in perl for some particular case makes the rule I gave unworkable in practice.

(Besides that, this proves that the general rule "A list in scalar context evaluates to its final element" is wrong, too.)

I don't understand how it proves this. Can you explain further?

The value of any expression evaluated in void context is discarded. This does not invalidate a rule defining what the value of the expression is. That perl is optimized to skip steps that have no side effects when evaluating an expression in void context does not, in my opinion, have any bearing on what the value of a LIST in scalar context is.

I am talking about the semantics of Perl and you seem to be talking about the operation of perl. It is natural then that we should have different opinions about what is or isn't and what happens.

Update: fixed a typo in the last sentence

Create A New User
Node Status?
node history
Node Type: note [id://720165]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2017-08-17 02:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (279 votes). Check out past polls.

Notices?