Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Inheritance and calling a subclass method *from* a baseclass method...

by merlyn (Sage)
on Mar 03, 2007 at 02:21 UTC ( [id://603011]=note: print w/replies, xml ) Need Help??


in reply to Inheritance and calling a subclass method *from* a baseclass method...

You should be calling _underthehood as a class method, not as a subroutine. Then the right thing would happen:
sub new { my $class = shift; ... my $self = $class->_underthehood(); ... } sub _underthehood { my $class = shift; die "subclass responsibility"; }

Replies are listed 'Best First'.
Re^2: Inheritance and calling a subclass method *from* a baseclass method...
by EvanK (Chaplain) on Mar 03, 2007 at 03:25 UTC
    Thanks Merlyn, that did it! Just one thing, since its being called as a method (with the indirection operator), it passes the classname as the initial parameter...There's no way around that, right?

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

      That's right; that's how you get the invocant in Perl.

      (As a side note, beware of the indirect constructor call; my $object = Some::Class->new() is much more reliable than my $object = new Some::Class(). The latter is very sensitive to order of declaration during parsing; best to avoid it where possible.)

Re^2: Inheritance and calling a subclass method *from* a baseclass method...
by izut (Chaplain) on Mar 03, 2007 at 22:33 UTC

    I would use Carp::confess in this case. And I probably wouldn't check if the method was overloaded on new(). Please consider the following example:

    package MyPackage; use strict; use warnings; use Carp qw(confess); sub new { my ($class) = @_; my $self = {}; bless $self, $class; return $self; } sub abstract_method { my ($self) = @_; my ($method) = ( caller(0) )[3]; confess "${method} should be overloaded."; } package main; my $p = MyPackage->new(); $p->abstract_method();

    Don't you think it is cleaner?

    Igor 'izut' Sutton
    your code, your rules.

      I think the two primary pieces of information I would want in a "method should be overloaded" error message are the name of the method and the name of the class that didn't implement it. So I really think you need to report the latter (my $class= ref $self || $self;).

      I'm not sure why you are writing code to look up the string "abstract_method" (perhaps so you can re-use this subroutine similar to *replace_me= \&abstrace_method;? -- no, doing so would still report "abstract_method" according to my testing, unfortunately). And, at least in this case, you don't even need to include the method name in the text since Carp::confess will already show that in the stack trace (though the error message is clearer if you do, of course). :)

      - tye        

        Actually the abstract_method name was used as example. Maybe was better if I used your_abstract_method_here or something.

        Igor 'izut' Sutton
        your code, your rules.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://603011]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found