hanenkamp has asked for the wisdom of the Perl Monks concerning the following question:
I know I'm stretching things a bit, but I really need the ability to conveniently wrap one object inside of another. In this way, the wrapper can then modify aspects of calls or returns meant for the wrappee. I'm doing this through the creation of a master wrapper class which then provides any derived wrapper with all the tools needed to perform the wrapping without all the nasty bits.
However, I've run into a problem whereby AUTOLOAD ends up getting called recursively when it seems that it should be calling a specific method in the derived class. The call is also invoking the "Use of inherited AUTOLOAD for non-method XXX is deprecated" warning, even though I'm using method call syntax.
Anyway, the code goes something like this:
package Wrapper; sub new { my ($class, $wrappee) = @_; bless { wrappee => $wrappee }, $class; } sub AUTOLOAD { my ($self, @args) = @_; my ($sub) = $AUTOLOAD =~ /([^:])$/; my $type = ...; # figured from other info $self->wrap($sub, $type, @args); } sub wrap { my ($self, $sub, $type, @args) = @_; if ($type = 'A') { $self->wrap_A($sub, @args); } elsif ($type = 'B') { $self->wrap_B($sub, @args); } elsif ($type = 'C') { $self->wrap_C($sub, @args); } else { die "Bad type $type"; } } # defaults just pass call onto wrappee. sub wrap_A { ... } sub wrap_B { ... } sub wrap_C { ... } package Wrapper::First; use base 'Wrapper'; # overrides wrap to wrap all method types sub wrap { ... } package Wrapper::Second; use base 'Wrapper'; # overrides wrap_B to wrap all methods of type B sub wrap_B { ... }
I'm not having a problem when I use code like Wrapper::First, but Wrapper::Second causes the warning and results in a call going to AUTOLOAD first, then to wrap, then instead of wrap_B in Wrapper::Second back to AUTOLOAD, which leads to deep recursion. (In my real code, I anticipated encountering this if I happened to have a misspelt method name, so I have a check that dies when the recursion depth gets too high.)
My real code is a lot more complicated than this, so I don't doubt that the real problem isn't shown here. However, does anyone know why the $self->wrap_B(...) calls are being interpreted as subroutine calls causing the inherited AUTOLOAD warning to popup? I think this must be the root of the problem, but I don't know why it's happening.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Psycho AUTOLOAD question
by chromatic (Archbishop) on Aug 08, 2003 at 16:50 UTC | |
|
Re: Psycho AUTOLOAD question
by BrowserUk (Patriarch) on Aug 08, 2003 at 17:09 UTC | |
|
Re: Psycho AUTOLOAD question
by hanenkamp (Pilgrim) on Aug 09, 2003 at 03:25 UTC | |
|
Re: Psycho AUTOLOAD question
by hanenkamp (Pilgrim) on Aug 09, 2003 at 03:58 UTC |