Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Dynamically wrapping ancestor method calls

by rafl (Friar)
on Dec 11, 2006 at 14:52 UTC ( #589053=note: print w/replies, xml ) Need Help??

in reply to Dynamically wrapping ancestor method calls

It was me who asked that question on #perl.

I could not think of a clean solution to solve that problem using subclassing. Therefor I now use a pattern like this:

sub new { my $class = shift; my $obj = SomeClass->new( @args ); bless \$obj, $class; } sub AUTOLOAD { ...; $$self->$meth( @_ ); ...; }

This creates a wrapper class around the class I initially tried to subclass and delegates all method calles to the inside object using AUTOLOAD. This works pretty well so far.

Cheers, Flo

Replies are listed 'Best First'.
Re^2: Dynamically wrapping ancestor method calls
by polettix (Vicar) on Dec 11, 2006 at 15:19 UTC
    You should probably try to figure out what to do with "implicitly defined" methods in your wrapper class, like can() and isa(). My skills don't allow me to either try to suggest something intelligent, or to assure that these two methods are the only ones that you get for free when you create a wrapper like this. Moreover, you should be sure that the already existing methods don't play with caller() (you could try to read Hook::LexWrap to figure out how to deal with this issue).

    Update: from chromatic's answer, I assume that I actually got all the implicitly defined subs :)

    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
      sub can { my $self = shift; ${ $self }->can( @_ ) } sub isa { my $self = shift; ${ $self }->isa( @_ ); }
Re^2: Dynamically wrapping ancestor method calls
by Limbic~Region (Chancellor) on Dec 11, 2006 at 14:56 UTC
    I intentionally didn't mention you specifically because you didn't post here when I suggested you to. Avoiding subclassing all together was one way I thought of too. I still want to know if anyone has a clean way of doing it as originally asked. Sometimes innovative thinking comes from unexpected places.

    Minor wording updates

    Cheers - L~R

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://589053]
[LanX]: nevermind
Tanktalus is at work, ssh'd home, using his cbstats bridge to talk on perlmonks CB. :)
[Tanktalus]: anyway, next is to figure out how to get postgres to agree that these are the same :)

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2018-03-21 15:56 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (269 votes). Check out past polls.