EvanK has asked for the wisdom of the Perl Monks concerning the following question:
I have plenty of procedural experience with Perl, but not that much OOP, and this is my first real foray into inheritance. I'm writing an abstract base class with two subroutines, a fully functional constructor, and an unimplemented method:
Ideally, someone could then subclass that base module, and just implement the _underthehood() method to return a hashref:package MyClass; # fully functional constructor method sub new { # if we're trying to instantiate the abstract class, die my $class = shift; die "abstract class must be subclassed" if $class eq 'MyClass'; # do some processing, all with lexically scoped variables # ... # call (as of yet) unimplemented function to return a hashref my $self = _underthehood(); die "_underthehood() not implemented" if ref $self ne 'HASH'; # bless hashref as instance of the current package, and return it bless $self, $class; return $self; } # unimplemented member method sub _underthehood { return; } 1;
Now, the subclass could be directly invoked, and all is well with the world:package MyClass::Implemented; use base 'MyClass'; # newly implemented member method sub _underthehood { # do some processing on the given arguments, #creating a populated hashref my $hashref = { 'key' => 'value' }; return $hashref; } 1;
But, as usual, I'm doing something wrong, because even though new() is invoked and has a class value of 'MyClass::Implemented', the script dies because _underthehood() is returning undefined, which means its actually calling MyClass::_underthehood(). So how can I get it to call MyClass::Implemented::_underthehood() from within the generic base class' constructor?#!/usr/bin/perl -w use strict; use MyClass::Implemented; my $obj = new MyClass::Implemented();
__________
Systems development is like banging your head against a wall...
It's usually very painful, but if you're persistent, you'll get through it.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Inheritance and calling a subclass method *from* a baseclass method...
by merlyn (Sage) on Mar 03, 2007 at 02:21 UTC | |
by EvanK (Chaplain) on Mar 03, 2007 at 03:25 UTC | |
by chromatic (Archbishop) on Mar 03, 2007 at 06:16 UTC | |
by izut (Chaplain) on Mar 03, 2007 at 22:33 UTC | |
by tye (Sage) on Mar 04, 2007 at 03:42 UTC | |
by izut (Chaplain) on Mar 04, 2007 at 12:36 UTC | |
by tye (Sage) on Mar 04, 2007 at 13:35 UTC | |
|
Back to
Seekers of Perl Wisdom