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

How do I do multiple inheritance in perl. In the example below I have an Employee class which I wanted to inherit from two classes Person and Gender. However, that does not seem to work. The first print statement works, I assume because the constructor is implicitly called. The second print statement does not?

Any suggestions, would be greatly appreaciated?</>

Thanks Robert Walkup


rdww@ti.com
#!/bin/perl package main; #main use Employee; my $hld = new Employee(); print "my gender is " . $hld->gender() . ".\n"; print "my name is " . $hld->fullname() . ".\n"; exit(0); #Employee - Inherits from Person Class and Gender Class package Employee; use Gender; use Person; @ISA = ("Gender","Person"); 1 #Person Class - just sets full name package Person; use strict 'vars'; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{FULLNAME} = "Robert Walkup"; bless ($self, $class); return $self; } sub fullname { my $self = shift; return $self->{FULLNAME}; } 1 #Gender Class - just sets full gender package Gender; use strict 'vars'; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{GENDER} = "MALE"; bless ($self, $class); print "YEAKS\n"; return $self; } sub gender { my $self = shift; return $self->{GENDER}; } 1

Replies are listed 'Best First'.
Re: how do I do multiple inheritance
by Flame (Deacon) on Jan 25, 2002 at 02:23 UTC
    Since there's no constructor in the Employee class, it's going to run the first one it comes across in @ISA, not ALL of them, just the first. Quite simply, it's never running the other constructor.

    Edit: Fixed misprint *I'm half asleep here*. Thanks VSarkiss.



    "Weird things happen, get used to it."

    Flame ~ Lead Programmer: GMS

Re: how do I do multiple inheritance
by lachoy (Parson) on Jan 25, 2002 at 02:57 UTC

    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

      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

(tye)Re: how do I do multiple inheritance
by tye (Sage) on Jan 25, 2002 at 22:42 UTC

    I'd like to mention that this is a classic example of how inheritance is taught in OO text books and is also a classic example of how to not use inheritance in the real world.

    I wish I could eloquently explain why this is such a bad idea. The basic problem is that inheritance is a very tight binding between types and it can easily tie you up down the road and make things very difficult. You are usually better off having one object contain a reference to another over having the one inherit from the other.

            - tye (but my friends call me "Tye")
      Howdy!

      I'll take a whack at it...

      If you say

      package Employee; use Person; use Gender; @ISA = qw/Person Gender/;
      you are saying that an Employee is both a Person and a Gender.

      You probably want something where Gender is an attribute of Person or Employee. Something like:

      package Person; use Gender; sub new { ... $self->{gender} = Gender->new; ... }

      Now, Persons have among their attributes a Gender. But a Person is not a Gender. HAS-A versus IS-A.

      yours,
      Michael