Dr. Mu has asked for the wisdom of the Perl Monks concerning the following question:

I have an object class, Loop, which inherits from AtomBlock, which inherits from Atom. There is another object class, Subroutine which uses the same inheritance chain as Loop. In each case, the inheritance chain is defined with the use base pragma. In Atom.pm there's an object method called "child", which is used to populate the various objects with subordinate objects. Both Loop.pm and Subroutine.pm call child as an object method, as part of their new class methods.

Subroutine::new is called as:

my $newobject = Subroutine->new($parent)
Loop::new is called as:
my $class = 'Loop'; my $newobject = $class->new($parent)
Both calls are made from the same module, Worksheet.pm. Subroutine::new works just fine, but Loop::new returns an error, "Undefined subroutine &Loop::child ...".

I realize that use base is a compile-time pragma, so I've made sure to include a use Loop; in both the main program and Worksheet.pm. But what else have I overlooked?

Update:

Oh gosh, this is embarassing. I'm glad I'm among friends! Anyway, the invocation of child that caused the error cited was not in Loop::new, but later in Loop::edit, viz:

sub edit { my $self = shift; $self->(child('atoms')->[0])->edit }
The parenthesis to the left of child should be to the left of $self; or else the parens should be left out entirely: they're superfluous. With the change, the program worked. Without it, child was not being invoked as a method of $self, but as a simple subroutine in Loop.pm. Hence the error. My apologies for wasting the monks' time on this one!

Replies are listed 'Best First'.
Re: Inheritance Issues
by Tanktalus (Canon) on Jan 27, 2005 at 22:36 UTC

    Maybe it's just me, but I think it'd be really handy if you could post a minimal-working (or not-working) example with everything in it. I doubt that we need to go all the way up to Atom, but basically, S and L, deriving from AB, even if the subs are as trivial as sub child { print "in child\n" }. Then post what the output is, and what you expect, and that would make it much easier to figure out.

    Thanks

      Yes, I do agree that would be handy. However, even a minimal working example with the requisite multiple modules would take some significant effort to prepare and debug. So I was hoping my brief description would strike a chord with someone out there. If not, I shall make the required effort to produce a working example.

      Thanks.

        However, even a minimal working example with the requisite multiple modules would take some significant effort to prepare and debug.
        Asking a question here doesn't mean to spend no effort at all beforehand.

        That's part of the solution. Probably everybody here has experienced this once - you have a problem, you try to explain it so everybody will understand, and suddenly you come up with the solution yourself. =)