in reply to Re: Overriding 'static' methods of a perl package
in thread Overriding 'static' methods of a perl package

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)

Replies are listed 'Best First'.
Re^3: Overriding 'static' methods of a perl package
by Corion (Patriarch) on Sep 16, 2008 at 14:48 UTC

    "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.

        As you keep the exact text of "compilation error" to yourself, I won't be able to help you with that. The most likely reason is that parent.pm is not installed on your machine. You could try the substitute base.pm that is distributed with older versions of Perl.

        I can only iterate it again. If you want to use features such as "inheritance", you'll have to use the object syntax everywhere.

        Of course, for customizing the behaviour of the One Big Subroutine, it's likely easier to pass it a callback and in its absence to use a default routine:

        package Parent; sub name { return 'Parent' }; sub identify { my (%options) = @_; $options{ get_name } ||= \&name; print "Hello " . $options{ get_name }->() . "\n"; }; package Child; sub identify { Parent::identify( get_name => sub { 'Child' }); }; 1;