Velocity Reviews > Perl > List and scalar OOE

# List and scalar OOE

nemo@weathersong.net
Guest
Posts: n/a

 10-17-2005
I don't understand the seeming discrepancy between this:

@a = ('foo', 'bar', 'baz');
@b = (@a, shift @a);

and this:

\$a = 5;
@b = (\$a, ++\$a, \$a++);

In the first case, OOE is intuitive. @b is first assigned the value -as
it is then- of @a, and then the value of "shift @a", so @b ends up
being foo, bar, baz, foo. In the second case however, the list elements
seem to be evaluated in advance (or, as if they were now pointers, not
copies), so @b is 7 7 6, instead of 5 6 6.

Is the discrepancy intentional? That is, OOE behaves differently on
lists and scalars? Or is there some other subtle difference I'm missing
completely, here?

usenet@DavidFilmer.com
Guest
Posts: n/a

 10-17-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> \$a = 5;
> @b = (\$a, ++\$a, \$a++);
> ... In the second case however, the list elements
> seem to be evaluated in advance (or, as if they were now pointers, not
> copies), so @b is 7 7 6, instead of 5 6 6.

> Is the discrepancy intentional? That is, OOE behaves differently on
> lists and scalars? Or is there some other subtle difference I'm missing
> completely, here?

I think the behavior is attributed to the fact that the "++" operator
has a very high order of precedence, whereas a comma operator (as a
list element separator) has a rather low order of precedence. The
expression is evaluated according to precedence, so the "++" gets
evaluated first, before the "listification" happens.

nemo@weathersong.net
Guest
Posts: n/a

 10-17-2005
Ahh, it's not OOE at all. I was overthinking it. Thanks.

Guest
Posts: n/a

 10-17-2005

(E-Mail Removed) wrote:
> Ahh, it's not OOE at all. I was overthinking it. Thanks.

i think Perl looks at the statement from right to left. is it really a
matter of binding precedence?

nobull@mail.com
Guest
Posts: n/a

 10-18-2005
(E-Mail Removed) wrote:

> I don't understand the seeming discrepancy between this:
>
> @a = ('foo', 'bar', 'baz');
> @b = (@a, shift @a);
>
> and this:
>
> \$a = 5;
> @b = (\$a, ++\$a, \$a++);
>
> In the first case, OOE is intuitive.

For an explanation of OOE with respect to ++ see the old thread "When
does (pre|post)increment happen?"