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

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

Thread Previous | Thread Next
From:
Jan Dubois
Date:
July 15, 2010 11:31
Subject:
RE: Order of evaluation of terms (was peephole optimiser could prune more dead code)
Message ID:
020301cb244b$e35337a0$a9f9a6e0$@activestate.com
On Thu, 15 Jul 2010, Zefram wrote:
> David Golden wrote:
> >Again, from perlop:
> >
> >       Operator precedence means some operators are evaluated before others.
> >       For example, in "2 + 4 * 5", the multiplication has higher precedence
> >       so "4 * 5" is evaluated first yielding "2 + 20 == 22" and not "6 * 5 ==
> >       30".
> 
> In that expression, the * operation does have to be performed before the
> + operation.  This does not imply anything about the order in which 2,
> 4, and 5 are evaluated (which would matter if they were expressions with
> side effects).

And that is also what Perl currently does:

$ perl -E 'sub AUTOLOAD { say $AUTOLOAD; ++$i } say a() + b() * c()'
main::a
main::b
main::c
7

So it does call a() first, even though the multiplication of b() and c()
happens before the result is added to a(). But there is nothing in the
operator precedence description that would forbid Perl from delaying
evaluation of a() until after b() * c() has been computed.

Cheers,
-Jan



Thread Previous | 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