in reply to Re: Class Inheritance without an instance
in thread Class Inheritance without an instance / $AUTOLOAD equ

Here is the difference from your code:

I am stuck with this kind of call:

package main; B::foo();
rather than this kind:
package main; B->foo();
which would be the same in practice (I believe) as:
package main; B::foo(B);
My problem is that neither the class name nor an instance are being passed. But it's still originally called as B::foo, so when I get to A::foo, it seems like it should be _somewhere_ that it was B::foo at first.

Replies are listed 'Best First'.
Re^3: Class Inheritance without an instance
by dave_the_m (Monsignor) on Oct 13, 2005 at 11:58 UTC
    B::foo(B) does not invoke inheritance. If A::foo ends up being executed, then it's not as a direct result of calling B::foo(B).

    (The slight exception to the the above is that AUTOLOAD is still inherited for non-method calls, although it's deprecated:

    package A; sub foo { print "A::foo called\n" } sub AUTOLOAD { print "A::AUTOLOAD called, \$AUTOLOAD=$AUTOLOAD\n" } package B; @ISA = qw(A); package main; B::foo('B'); __END__ $ perl /tmp/p Use of inherited AUTOLOAD for non-method B::foo() is deprecated at /tm +p/p line 11. A::AUTOLOAD called, $AUTOLOAD=B::foo $

    Dave.

      I think using an AUTOLOAD method to catch the apache handler call is the way to go here. Thank you for writing this out, it made it clear to me how easy that would be.
        Note that relying on AUTLOAD behaviour for non-method calls is bad. It's deprecated. It's only there because of a historical bug in the perl interpreter. It's a feature that may get removed at some point.

        Dave.

Re^3: Class Inheritance without an instance
by Perl Mouse (Chaplain) on Oct 13, 2005 at 12:00 UTC
    If B inherits A, and B::foo isn't defined, but A::foo is, calling B::foo will fail. There's no such sub.

    If you want Perl to act in an OO way, you should make a method call, not a sub call. Use B->foo, and A::foo will be called.

    If you can't or won't, you're out of luck.

    use strict; use warnings; $| = 1; sub A::foo {print "This is A::foo\n"} @B::ISA = qw /A/; print "Calling B->foo(): "; B->foo(); print "Calling B::foo(): "; B::foo(); __END__ Calling B->foo(): This is A::foo Calling B::foo(): Undefined subroutine &B::foo called at ...
    Perl --((8:>*
Re^3: Class Inheritance without an instance
by dynamo (Chaplain) on Oct 13, 2005 at 11:57 UTC
    That was my best idea, given my understanding of mod_perl, but now i'm not as sure. I just tested what I did above and got an error that wasn't present in the apache logs. The interface is the same as any other mod_perl handler:

    sub handler { my $r = shift; my $err = ErrorTracker->new; # ... setup stuff my $args = validate_args($r, wrapped_handler_args()); $r->content_type('text/plain'); wrapped_handler($r,$args,$err); # shutdown / error handling stuff return $err->apacheStatus(); } sub wrapped_handler { print "inheritance is broken"; } sub wrapped_handler_args { return {}; }