in reply to Re^4: perl inheritance
in thread perl inheritance

Assuming your constructor, new, can create new objects when given the appropriate arguments:

package ReblessKids; use strict; use warnings; use Scalar::Util 'blessed'; sub do_something { my $self = shift; ... my $class = blessed $self; return $class->new( ... ); }

Update: I did mean blessed, not reftype.

Replies are listed 'Best First'.
Re^6: perl inheritance
by Anonymous Monk on Mar 29, 2012 at 15:39 UTC
    in other words, I'm getting the following error when I try to call $class->new(...)
    Can't locate object method "new" via package "HASH" (perhaps you forgo +t to load "HASH"?) at Table.pm line 34. at Table.pm line 34
    I thought that reftype would return the polymorphic type (i.e. the subclass). Sorry Chromatic, but I'm bit confused here..
      Yes, reftype() returns the type of the variable. I think what was meant was blessed() which returns the name of the package that it is blessed into:
      my $class = blessed $self;
Re^6: perl inheritance
by Anonymous Monk on Mar 29, 2012 at 14:43 UTC
    $class = reftype $self
    This seems to be return 'HASH' and not the subclass. What am I doing wrong?