in reply to Inheritance - parent of parent (solved)

Passing a partially constructed object down to the parent's constructor is very weird.
{ package ClassA; ... sub new { ... my $self = bless({}, $class); $self->{...} = ...; return $self; } ... } { package ClassB; our @ISA = 'ClassA'; sub new { ... my $self = $class->SUPER::new(); $self->{...} = ...; return $self; } ... } { package ClassC; our @ISA = 'ClassB'; sub new { ... my $self = $class->SUPER::new(); $self->{...} = ...; return $self; } ... }

No infinite loops, though. Not in your version or mine.

Replies are listed 'Best First'.
Re^2: Inheritance - parent of parent
by jockel (Beadle) on Nov 23, 2007 at 08:08 UTC

    Hmm..

    Strange that the infinite loop don't occur.., good, but strange.

    I must admit that I didn't try the code above myself,, but

    copied the important parts from my real script, where it loops infinitly..

    My assumptions about the infinite loop are because since the variable $class holds 'CLASS_C', so that calling

    $class->SUPER::new();

    would be the same as calling

    CLASS_C->SUPER::new();

    And the 'SUPER' of CLASS_C is CLASS_B, right?

    So, shouldn't that result in new() in CLASS_B calling itself?

    I've had kind of a good sleep, so now I'm going to look through my code again..

    Thanks for the input so far =)

      No, SUPER is based on __PACKAGE__, not on ref($self). Quote perlobj,

      It is important to note that SUPER refers to the superclass(es) of the current package and not to the superclass(es) of the object.

      This is easy to demonstrate.

      { package Base; sub m { print("Boo!\n"); } } { package Child; our @ISA = 'Base'; bless({}, 'Anything')->SUPER::m(); # Boo! }