develooper Front page | perl.perl5.porters | Postings from July 2010

Order of evaluation of terms (was peephole optimiser could prune more dead code)

Thread Next
From:
David Golden
Date:
July 15, 2010 07:06
Subject:
Order of evaluation of terms (was peephole optimiser could prune more dead code)
Message ID:
AANLkTilYOq-b8X31H1FFoU4w3R-zD6l5S9PlER6rTMbN@mail.gmail.com
On Thu, Jul 15, 2010 at 9:33 AM, demerphq <demerphq@gmail.com> wrote:
>> On the other hand, it may not be documented or specified, but I think most
>> people would expect that in the following, f() is called before g():
>>    $f() . $g()
>
> Hmm. I don't know that I would. If we want this to be the case then
> IMO we should document it.

I thought this was actually specified in terms of precedence and
associativity, but if I read it narrowly as written, it's not.  At
best it's implied.

But maybe we can get there by analogy with some identity operations.
Consider this:

    q{} . $f() . $g()

Concatenation is left associative so C<< q{} . $f() >> is evaluated
first. A function call has higher precedence so $f() is evaluated
before the concatenation.  Only then is $g() evaluated.

Or consider this:

    $f() ** $g() ** 1

Exponentiation is right associative so C<< $g() ** 1 >> is evaluated
first.  Function call has higher precedence so $g() is evaluated
before exponentiation.  Only then is $f() evaluated.

The implication is that in any binary operation, the terms are
evaluated in order of the associativity of the operation.

I don't know if that's guaranteed to be true in the interpreter.  If
it *is*, then we should add a note to that effect in the perlop
paragraph on operator associativity.  If *not*, then I wonder why not
and what it would take to make it true.

The other thing I wonder is whether clarifying precedence of variable
terms (including resolution of ties or magic) versus other terms would
help resolve the ambiguities in any way, but I haven't really followed
that thought through.

-- David

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About