alafarm has asked for the wisdom of the Perl Monks concerning the following question:

I'm troubleshooting some old code and I'm getting mixed up about method inheritance. My example is summarized below, which represent two packages in different .pm files.

Package two inherits from package one. But when the "run" method is called, which in turn calls "_dosomething", the AUTOLOAD sub runs instead of "_dosomething". Does the call to "_dosomething" need to be prefixed by an explicit package name or self object?

(Of course the problem could be something else, but I want to make sure my understanding of inheritance is correct.)

package one; sub _dosomething {...}; sub AUTOLOAD {...}; package two; use one; @ISA = qw(one); sub run { _dosomething ... }

Replies are listed 'Best First'.
Re: Method inheritance
by moritz (Cardinal) on May 11, 2012 at 17:21 UTC

    Inheritance only works for method calls, but what you do is a subroutine call.

    This should work intead:

    sub run { my $self = shift; $self->_dosomething; ... }

    Or you can directly call one::_dosomething, but then there's no need for inheritance in the first place.

      Thanks. What was confusing me is that (correct me if I'm wrong), if these two modules were not specified as "packages" and without the "@ISA", then the subroutine call would have worked.
Re: Method inheritance
by jdporter (Paladin) on May 11, 2012 at 17:28 UTC
    Does the call to "_dosomething" need to be prefixed by an explicit package name or self object?

    Yes.

    Use of inherited AUTOLOAD for non-method two::_dosomething() is deprec +ated

    AUTOLOAD is for function resolution; inheritance is for method resolution. If you're going to mix them, you need to be very careful.

    I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.