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.


In reply to Inheritance and calling a subclass method *from* a baseclass method... by EvanK

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.