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:
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;
Ideally, someone could then subclass that base module, and just implement the
_underthehood() method to return a hashref:
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;
Now, the subclass could be directly invoked, and all is well with the world:
#!/usr/bin/perl -w
use strict;
use MyClass::Implemented;
my $obj = new MyClass::Implemented();
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?
__________
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.