in reply to Re: Re: maintaining constructor inheritance cascade
in thread maintaining constructor inheritance cascade

Ok. I get the picture now. (Sounds like a goofy design to me, but whatever.)
package myParent; our @ISA; sub new { my $class = shift; bless $self,$class; $self->_parent_init(@_); return $self; } sub _parent_init { my $self = shift; print "_initParent (@_)\n"; } #---------------------------------- package myChild; our @ISA; use base qw(myParent); sub new { my $class = shift; my $self = $class->SUPER::new; $self->_child_init(@_); return $self; } sub _child_init { my $self = shift; print "_initChild (@_)\n"; }
Etc. The idea is that your constructor is a wrapper on your parent's constructor, but you do additional things after that's called. So, the delta off your original code is that there is only one bless - in the ultimate parent's code. (You were doing a bunch of reblessing, which I think was confusing you.)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re2: maintaining constructor inheritance cascade
by tilly (Archbishop) on May 14, 2003 at 15:30 UTC
    Note that the poster asked about multiple inheritance. To handle that use TheDamian's module NEXT and then call NEXT instead of SUPER.

    Unfortunately that does have to be done in all modules in the inheritance tree. If this is not acceptable, the asker will likely have to examine all of the classes @ISAs to find all of the init methods and then call them all in turn.

Re: Re2: maintaining constructor inheritance cascade
by jaa (Friar) on May 14, 2003 at 15:31 UTC
    Excellent, it is an improvement - so I end up with proper cascading constructors using:
    sub new { my $class = shift; my $self = $class->SUPER::new(@_); $self->_initChild(@_); return $self; }

    Which is cleaner than looking at @ISA for a possible parent.

    Thanks!

      Well, actually, SUPER does look at @ISA ... more significantly, perhaps, it looks at the @ISA in the current package, which may have nothing at all to do with the actual class of the object that you may or may not be working with at a given time. But oddly enough that's usually the right thing. Weird.

          -- Chip Salzenberg, Free-Floating Agent of Chaos