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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.