Thanks for all the replies. The problem that served as context for the question is to devise an automatic way to define delegated methods.

A typical "delegated method" looks something like:

sub frobozz { my $self = shift; return $self->{ _delegate }->frobozz( @_ ); }
...where $self->{ _delegate } holds a reference to some other object. The first object basically passes the buck to the delegate.

My basic idea was to run through all the "public methods" that the delegate can perform (by plucking them out of its and its ancestors' symbol tables) and define a delegated method (like the one illustrated above) for each of those methods among these that the containing class cannot perform.

The fly in the ointment, of course, is that fishing functions out of symbol tables, in general, cannot distinguish between public methods (which are part of the delegate's API) and private methods or functions that have been imported by the delegate class, and neither of which belong to its API.

The only thing I can think of is that, if all the ancestor classes are ones that I control, then I can define those classes so that their private methods are easy to distinguish (e.g. with leading underscores) and so that they don't import anything. They either use fully qualified function names, or do something like:

BEGIN { *_croak = \&Carp::croak; }
I.e. import a function "by hand" but rename it in such a way that it can be easily filtered out.

(Or I can go nuts and assign all these non-API methods to lexical scalars.)

And yes, the whole scheme described above would miss any AUTOLOADed or runtime-defined methods of the delegate object. Again, I don't know a way around this, other than avoiding using such methods in those classes I control.

I suspect that, as is my habit, I am complexifying things... As always, your comments are most welcome.

the lowliest monk


In reply to Re: defined &{ $pkg . '::foo' } vs. $pkg->can( 'foo' ) by tlm
in thread defined &{ $pkg . '::foo' } vs. $pkg->can( 'foo' ) by tlm

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.