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.


In reply to Psycho AUTOLOAD question by hanenkamp

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.