Yes, it works. No, it isn't any slower than normal OO dispatch. But it isn't necessarily advisable.

That said, it does have its uses. For instance, in a system I'm working on we have a set of methods that we need to be able to call on any object, and which can and should be overridden where necessary in client software. There's another set of methods that need to be callable on any object that should be considered as 'final'; in other words, we don't want client code overriding them.

To do this, we take advantage of the way perl will allow you to call fully qualified packages. Our overrideable methods all get put in UNIVERSAL, with a px_ prefix to prevent method name collisions with other objects, and methods that we need to be final get put into a PIXIE::* package.

Why do this? Because we want to maintain uniform access to methods. We could just make subroutine calls on objects for the 'final' methods and method calls for those methods in UNIVERSAL but we took the view that we wanted to maintain OO calling style. If the method calling overhead proves to be a significant issue when we come to do profiling, then of course we'll look at switching to direct function calls, but for now we'll stick with method calls throughout.

Note too that we're careful, in writing our PIXIE::* methods to make them dependent only on other PIXIE::* and px_* methods, and not to go making assumptions about the internal structure of objects on which they are called.


In reply to Re: Invoking method in another package by pdcawley
in thread Invoking method in another package by nutshell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.