Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2018-01-17 13:52 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (200 votes). Check out past polls.