in reply to how do I do multiple inheritance

One way of getting around this: provide a base class which traverses the @ISA for a class and calls the constructor for each. (The Class::ISA module would prove very handy.) Using Class::Base for the base class may be a smart thing to do.

For a more elegant solution, you might be interested in the NEXT module by TheDamian, which allows you to do smart and (relatively) painless inheritance traversal.

Chris
M-x auto-bs-mode

Replies are listed 'Best First'.
Re: Re: how do I do multiple inheritance
by Vavoom (Scribe) on Jan 25, 2002 at 03:38 UTC
    The problem with calling multiple constructors is that it results in an object that only contains the results of the last constructor. What is really needed is a seperation of the creation and initialization steps in each class.

    Here is the method I use (essentially a cut and paste from Chapter 6 of Damian Conway's excellent book Object Oriented Perl) as it applies to this particular problem:
    #!/usr/bin/perl -w use strict; use Employee; my $hid = Employee->new; print "My gender is " . $hid->gender() . "\n"; print "My name is " . $hid->fullname() . "\n"; package Employee; use strict; use _Initializable; use Person; use Gender; @Employee::ISA = qw( _Initializable Person Gender ); sub _init { my $self = shift; $self->Person::_init(); $self->Gender::_init(); } 1; package Person; use strict; use _Initializable; @Person::ISA = qw( _Initializable ); sub _init { my $self = shift; $self->{FULLNAME} = "Robert Walkup"; } sub fullname { my $self = shift; return $self->{FULLNAME}; } 1; package Gender; use strict; use _Initializable; @Gender::ISA = qw( _Initializable ); sub _init { my $self = shift; $self->{GENDER} = "MALE"; } sub gender { my $self = shift; return $self->{GENDER}; } 1; package _Initializable; use strict; sub new { my $class = shift; my $self = {}; bless $self, ref($class) || $class; $self->_init(); return $self; } 1;
    As you can see, the only major change is the addition of the _Initializable class to handle the creation of new objects, and the _init methods to initialize said objects. For any method other than a constructor, the options listed by lachoy above should work as advertised.

    Vavoom

      Actually, the aforementioned Class::Base will do this for you automatically, and you won't need to specify the _init() method of your parents along the way. Sweet.

      Chris
      M-x auto-bs-mode