Greetings Monks!

I'm a little new to perl, so bear with me if my lingo is wrong. :)

I'm working with a large legacy code base and looking for a way to be able to "generate" new functions implicitly and dispatch them to existing functions via an algorithm at runtime. It's akin to an AOP thing.

For example, I might want to take a function like:

$value = Foo::bar($x);
... and provide a "logged" version of that call in the form:
$value = Logged::Foo::bar("/tmp/log", $x);
... without writing the wrapper for every single function.

Originally I had a version that worked using AUTOLOAD. This was almost EXACTLY what I wanted, except that I couldn't figure out how to get one AUTOLOAD function to dispatch for multiple packages.

Enter: UNIVERSAL::AUTOLOAD. :)

So UNIVERSAL::AUTOLOAD works like a charm except that I get a warning that using this to dispatch non-methods is deprecated (perl 5.8.3) and that the workaround is to, you guessed it, explicitly define the AUTOLOAD in each package that does any dispatching.

Is there another way to do this? To summarize, I need to be able to write a single function that's deferred to as the "last resort" for name-based dispatch so that I can shim in code that can rewrite the requests in order to augment them and dispatch them to other functions.

I'm looking forward to working this out to provide some generalized predicate-dispatch type notions in some later projects, but I don't want to go too far down an unsupported path if I can possibly find a supported one.

Thanks in advance!
zonk


In reply to Dynamic function dispatching... by zonk

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.