2 Updates (at the bottom)

I have 2 classes, one inherits from the other, call them Parent and Child (Child inherits from Parent), and Parent has an AUTOLOAD method, and so does Child. The problem is that an instance of Child will never call Parent::AUTOLOAD, because of the call heirarchy. For instance:

package Parent; ... sub AUTOLOAD { my $self = shift @_; (my $method = $AUTOLOAD) =~ s/^.*:://; return if $method eq "DESTROY"; if ( $method eq 'parent_method' ) { # do something } } ... package Child; ... sub AUTOLOAD { my $self = shift @_; (my $method = $AUTOLOAD) =~ s/^.*:://; return if $method eq "DESTROY"; if ( $method eq 'child_method' ) { # do something } }
So, when i have an instance of Child, and call parent_method() on it, there is no parent_method() found in Child, then its not found in Parent, then its not found in UNIVERSAL. But then, Child::AUTOLOAD() is found, and is invoked. But that AUTOLOAD doesnt know about parent_method(), so does nothing, and Parent::AUTOLOAD() is not invoked. To get around this, im thinking i should add a line to the end of Child::AUTOLOAD() like this:
package Child; ... sub AUTOLOAD { my $self = shift @_; (my $method = $AUTOLOAD) =~ s/^.*:://; return if $method eq "DESTROY"; if ( $method eq 'child_method' ) { # do something } ... else { $self->SUPER::AUTOLOAD($method, @_); } }
Is this the proper way to do this, or am i missing something obvious?

Thanks much, as always.

Update: I found that i need to modify Parent::AUTOLOAD() too, so im thinking there is a better way than this, but heres the parent modification:

package Parent; ... sub AUTOLOAD { my $self = shift @_; my $method; if ( defined $AUTOLOAD ) { ($method = $AUTOLOAD) =~ s/.*:://; } else { $method = shift @_; } ... }
This had to be done because $Parent::AUTOLOAD is not set like for a normal call to AUTOLOAD, so i had to do something to get Parent::AUTOLOAD() to know what method to use.

Update 2:
I came up with a better way to do this, which doesnt require modifying Parent::AUTOLOAD():

# in Child::AUTOLOAD() # instead of $self->SUPER::AUTOLOAD($method, @_); my $super_method = 'SUPER::' . $method; $self->$super_method(@_);
The only thing about this one is that Perl will search through Parent and its parents, before calling Parent::AUTOLOAD(), which is a little more time consuming, but i am going to have the AUTOLOADS screw with the symbol tables and install AUTOLOAD'ed methods after their first invocation, which will speed things up.

In reply to AUTOLOAD cascade by shemp

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.