[comp.lang.perl] operl - programming tip #1

sakoh@sraco2.us.sra.co.jp (Hiroshi &) (02/23/91)

Hello folks, thank you for your heartful responses to OPERL.

This is a simple tip for OPERL programming.

--

Since &send() hires dynamic-binding scheme, it spoils its performance somehow.
If you are sure which method will be called in specific cases, you can reduce
that overhead using static-binding scheme.

For example, please suppose you have class definitions like:

    &defclass ('parent', 'root');
    &defmethod('parent', 'say', 'print "$_[0]!\n";');
    &defclass ('heir',   'parent');

Now you can create an instance and send a message to it.

    $h = &newobject('heir');
    &send($h, 'say', 'hello');

In this case, 'say' method will be searched in 'heir' class at first,
then found in 'parent' class. The method is bound dynamically.
However, as you can see easily, 'say' method of 'parent' class
will always be invoked in this example.
In such case, you can use static-binding to improve performance as following:

    $h = &newobject('heir');
    &parent'say($h, 'hello'); # NB: We don't use &send() here.

Of course, you can't use this technique when you are not sure which
method would be called eventually.
Actually, this trick should not be recommended
from the point of view of software engineering. (But I know you like it :-)
In usual OO languages, this kind optimization will be done by their
compilers not by human beings.
Writing such a compiler for OPERL? Too much work ...
(I didn't spend more than 3 hours on the implementation of OPERL ...)

Anyway, have fun.

with My Best Regards,
Hiroshi Sakoh
--
 sakoh@sra.co.jp
"Whereof one cannot speak, thereof one must remain silent."  ---Wittgenstein
"Sometimes noise is significant."                            ---William Hewlett