Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by izut (Chaplain)
on Mar 03, 2007 at 22:33 UTC ( #603091=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^3: Inheritance and calling a subclass method *from* a baseclass method... (error data)
by tye (Sage) on Mar 04, 2007 at 03:42 UTC

    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.

        I wasn't particularly confused on that point and another choice of example name wouldn't have eliminated my question: Why would you choose to write:

        sub whatever { #... my ($method) = ( caller(0) )[3]; #... }

        when it appears to be functionally equivalent to the trival and thus clearer:

        sub whatever { #... my $method= "whatever"; #... }

        - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2021-02-25 02:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?