I am doing a Perl OO example (mainly to reacquaint myself with Perl) and using
inheritance, it seems that the SUPER works in the subclass when calling the superclass constructor and that
the superclass variables don’t bind to the subclass object. Why would this be?
################################################
## Package : Person ##
################################################
package Person;
################################################
## Constructor ##
################################################
sub new(){
my $class = shift;
my $data = {};
$data->{'firstName'} = shift;
$data->{'secondName'} = shift;
bless $data, $class;
#bless $data, "Person"; #hack
return $data;
}
################################################
## Getter Methods ##
################################################
sub getFirstName(){
my $data = shift;
return $data->{'firstName'};
}
sub getSecondName(){
my $data = shift;
return $data->{'secondName'};
}
################################################
## Setter methods ##
################################################
sub setFirstName($){
my $data = shift;
$data->{'firstName'} = shift;
}
sub setSecondName($){
my $data = shift;
$data->{'secondName'} = shift;
}
################################################
## Other Methods ##
################################################
sub printall(){
my $data = shift;
$\ = "\n";
print "FirstName: ". $data->{'firstName'} ."\n";
print "SecondName: ". $data->{'secondName'} ."\n";
}
1;
################################################
## Package : Coder ##
################################################
package Coder;
@ISA = qw( Person );
use strict;
use warnings;
################################################
## Constructor ##
################################################
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
my $superFirstName = shift;
my $superSecondName = shift;
print "new superfirstname " .$superFirstName;
print "new supersecondname " .$superSecondName;
$self->{'language'} = shift; #i.e. Java
$self->{'experience'} = shift; #number of years
#$self = $self->SUPER::new($superFirstName, $superSecondName);
Person->new($superFirstName, $superSecondName);
return $self;
}
################################################
## Getter Methods ##
################################################
sub getLanguage(){
my $data = shift;
return $data->{'language'};
}
sub getExperience(){
my $data = shift;
return $data->{'experience'};
}
################################################
## Setter methods ##
################################################
sub setLanguage($){
my $data = shift;
$data->{'language'} = shift;
}
sub setExperience($){
my $data = shift;
$data->{'experience'} = shift;
}
################################################
## Other Methods ##
################################################
sub printall(){
my $data = shift;
$\ = "\n";
print "Experience: " . $data->{'experience'};
print "Language: " . $data->{'language'};
$data->SUPER::printall();
}
1;
################################################
## Package : Main ##
################################################
package main;
my $developer = Coder->new("John","Smith","Perl","2");
$developer->printall();
It’s because you are not doing anything with the
Person->newreturn. So it just creates a whole otherPersonobject, andCoderskates off happily.What you want to do is
And have
Personbless it into whatever class you pass it–as it does. Then after creating$selftheSUPERway, you want to add to it the fields you need.So it should look something like: