in reply to Overriding 'static' methods of a perl package

For that to work, sub identify must be written differently, as a class method:

package MyParent; sub identify { return $_[0]->name; }; sub name { return 'MyParent'; # or alternatively, return ref $_[0] || $_[0] }; package Child; use parent -norequire => 'MyParent'; sub name { return 'Child'; # or alternatively, return ref $_[0] || $_[0] };

Update: Also set up inheritance between Child and MyParent.

Replies are listed 'Best First'.
Re^2: Overriding 'static' methods of a perl package
by Anonymous Monk on Sep 16, 2008 at 14:46 UTC
    Let me clarify my omissions from my prevous post. Here's what I have at the present time:
    package Parent; sub identify { return name(); } sub name { return 'Parent'; } 1; package Child; use Parent; @ISA = qw(Parent); sub name { return 'Child'; } 1
    Parent::identify returns 'Parent' Child::identifiy returns nothing Parent::identify() returns 'Parent' Child::identify() generates an error (undefined subroutine)

      "You can't get there from here".

      Unless you use "object notation", and call your methods with a class or object instance, no inheritance will work.

      You could fake the stuff you want by trying AUTOLOAD trickery but down that road lies so much sorrow I won't even tell you how to do it.

      So you'll have to change Parent and Child in the way I outlined above.

        Ok.

        The line 'use parent -norequire => "Parent"' generates a compilation error.

        I guess I'm still dragging too much Java baggage along with me. In my 'real life' implementation of this idea, the 'Parent' package contains an elaborate routine that calls a number of supporting subroutines to control its operation. The idea was to override the supporting subroutines to create new behaviors.

        I was trying to avoid making it truly OO as there was only one use of the class after it was created.