Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
go ahead... be a heretic
 
PerlMonks  

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


Comment on Re: Dynamically wrapping ancestor method calls
Download Code
Re^2: Dynamically wrapping ancestor method calls
by Limbic~Region (Chancellor) on Dec 11, 2006 at 14:56 UTC
    rafl,
    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

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 :)

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

    Don't fool yourself.
      sub can { my $self = shift; ${ $self }->can( @_ ) } sub isa { my $self = shift; ${ $self }->isa( @_ ); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://589053]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-04-23 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (553 votes), past polls