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 | |
by ikegami (Patriarch) on Nov 23, 2007 at 02:40 UTC | |
by Joost (Canon) on Nov 23, 2007 at 02:55 UTC | |
by Crackers2 (Parson) on Nov 23, 2007 at 03:12 UTC | |
by Joost (Canon) on Nov 23, 2007 at 03:17 UTC | |
|
Re: Inheritance - parent of parent
by ikegami (Patriarch) on Nov 23, 2007 at 02:36 UTC | |
by jockel (Beadle) on Nov 23, 2007 at 08:08 UTC | |
by ikegami (Patriarch) on Nov 23, 2007 at 08:33 UTC | |
|
Re: Inheritance - parent of parent
by jockel (Beadle) on Nov 23, 2007 at 09:01 UTC |