Kinda messy. I know there's a better way to do it, but I don't have the reference book with me. Here's what I came up with:
package A; sub bar { return "BAR"; } sub AUTOLOAD { my (@objpath) = split(/::/, $AUTOLOAD); my $fname = pop @objpath; return if $fname eq 'DESTROY'; if ( $fname eq 'baz' ) { my $subr = <<EOSUB; sub $AUTOLOAD { return "FOO"; } EOSUB eval $subr; goto &$AUTOLOAD; return $subr; } return; } package B; @ISA = qw( A ); sub new { return bless {}, shift; } sub AUTOLOAD { my (@objpath) = split(/::/, $AUTOLOAD); my $fname = pop @objpath; return if $fname eq 'DESTROY'; if ( $fname eq 'foo' ) { my $subr = <<EOSUB; sub $AUTOLOAD { return "FOO"; } EOSUB eval $subr; goto &$AUTOLOAD; } else { return &{"A::$fname"}; } return; } package main; my $b = new B; print "foo: ", $b->foo, "\n"; print "bar: ", $b->bar, "\n"; print "baz: ", $b->baz, "\n";
The biggest change I made was adding an else clause to B::AUTOLOAD(). If the appropriate subroutine isn't found in package B, it constructs a symbolic reference to a function in A. Since that function may not be there, it's trapped by AUTOLOAD.

I have no idea whether or not this gets caching, but I'll check. It's not very clean or elegant or safe, so I wouldn't use it except in an emergency. Still, it's One Way To Do It.


In reply to Re: AUTOLOAD functions & inheritence by chromatic
in thread AUTOLOAD functions & inheritence by LunaticLeo

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.