in reply to Perl and common subexpressions

Unfortunately, unlike C, Perl has to contend with something a little crazier: Tied hashes and arrays.

And function calls. In

t = x->y->z->a->b[5];
any of y, z, or a can be methods, with or without side-effects, and with no guarantee that they'll return the same thing on subsequent calls. In the face of such flexibility, static analysis optimizations are non-starters.

Replies are listed 'Best First'.
Re^2: Perl and common subexpressions
by theorbtwo (Prior) on Dec 23, 2004 at 15:08 UTC

    It's trivial to tell the difference between a method call and a hash lookup for the perl compile -- you're just having issues with C vs perl syntax. (Getting e, an element of the struct pointed to by p in C is p->e, which looks a lot like calling a method, m, on an object $o in Perl -- $o->m)

    On the other hand, when it /is/ a method call, perl can't tell /what/ method is being called, because it doesn't know the type of the object you're calling the method on until runtime. The m method on Foo::Bar might be side-effect free, while the m method on Baz has side-effects.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).