Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Inheritance - parent of parent (solved)

by jockel (Beadle)
on Nov 22, 2007 at 23:31 UTC ( [id://652460]=perlquestion: print w/replies, xml ) Need Help??

jockel has asked for the wisdom of the Perl Monks concerning the following question:

Hello all Monks

I've been banging my head for a few hours now and I really need some input from others..

For this example, say I have 3 classes.

CLASS_A

CLASS_B

CLASS_C

I want the inheritance to look like this.

CLASS_C ISA CLASS_B ISA CLASS_A

So that if I want to access a method in CLASS_A I want to be able to do.

my $instanceC = CLASS_C->new(); $instanceC->method_from_class_A();

So,, this is where i'm stuck..

I start from the bottom...

Code for CLASS_C

package CLASS_C; use CLASS_B; @ISA = ('CLASS_B'); use strict; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{attribut} = 1; $self = $class->SUPER::new(self => $self); return $self; } 1;

Code for CLASS_B

package CLASS_B; use CLASS_A; @ISA = ('CLASS_A'); use strict; sub new { my $proto = shift; my (%params) = @_; my $class = ref($proto) || $proto; my $self = $class->SUPER::new(); foreach my $attr (keys %{$params{'self'}}) { $self->{$attr} = $params{'self'}->{$attr}; } return $self; } 1;

Code for CLASS_A

package CLASS_A; use strict; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless($self,$class); return $self; } 1;

What I know:

Everything fails into a endless loop because of the call to SUPER::new inside CLASS_B definition calls itself... But how should I write it?

Regards

Joakim

UPDATE: I had a call inside CLASS_B::new which called a 'CLASS_C::new' which in turn called CLASS_B::new again .. forever..

Apperently I were to tired yesterday when trying this..

But I guess it's not a waste, hopefully this post will help someone in the future =)

Replies are listed 'Best First'.
Re: Inheritance - parent of parent
by Joost (Canon) on Nov 23, 2007 at 00:06 UTC
    So that if I want to access a method in CLASS_A I want to be able to do.

    my $instanceC = CLASS_C->new(); $instanceC->method_from_class_A();
    WHY?

    Really, you don't want to do this except, maybe, as a workaround, and then you can do $instanceC->CLASS_A::method_name()

    Anyway your code does not show the symptoms you're describing:

    Can't locate object method "method_from_class_A" via package "CLASS_C" + at test.pl line 5.
    In other words, instantiating the object does not lead to an infinite loop, even if and the code probably doesn't do what you think it does.

      WHY?

      WHY NOT! What's wrong with calling an inherited method? The OP didn't say anything about calling an overridden method.

Re: Inheritance - parent of parent
by ikegami (Patriarch) on Nov 23, 2007 at 02:36 UTC
    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.

      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! }
Re: Inheritance - parent of parent
by jockel (Beadle) on Nov 23, 2007 at 09:01 UTC

    UPDATE: I had a call inside CLASS_B::new which called a 'CLASS_C::new' which in turn called CLASS_B::new again .. forever..

    Apperently I were to tired yesterday when trying this..

    But I guess it's not a waste, hopefully this post will help someone in the future =)

    And I now know how SUPER actually works!

    I thought it depended on '$class'

    Thanks for all the help =)
    /joakim

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://652460]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-19 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found