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

Would you have the constructor take a base class object and convert it to the derived?

I'd get the class of the provided object, then call its constructor directly.

Something like the following...

No. You're overcomplicating this and confusing yourself. Please post the existing code that constructs a new object so we can all see what you're doing.

Replies are listed 'Best First'.
Re^4: perl inheritance
by Anonymous Monk on Mar 26, 2012 at 14:07 UTC

    My sincere apologies for replying to you so late, Chromatic. Unfortunately, I don't have decent code worth posting at this point. Would you possibly have any example classes that you can point me to that would demonstrate what you are referring to..

    many thanks as usual. Michael

      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.

        $class = reftype $self
        This seems to be return 'HASH' and not the subclass. What am I doing wrong?
        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..