in reply to Re: perl inheritance
in thread perl inheritance

I'm not sure exactly how I should do that. Would you have the constructor take a base class object and convert it to the derived? Something like the following:

sub new { if (ref $class eq 'base') { //return derived object built from base class object } //return derived object built from parameters }
Would invoking this form of the derived class constructor through AUTOLOAD be considered bad practice? This is so that all base class methods do not have to be overridden?

Replies are listed 'Best First'.
Re^3: perl inheritance
by chromatic (Archbishop) on Mar 23, 2012 at 22:02 UTC
    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.

      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.