CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Getting a superclass' variables printed out

 



wtritchie
New User

Jul 1, 2010, 8:56 AM

Post #1 of 2 (591 views)
Getting a superclass' variables printed out Can't Post

 
I am doing a Perl OO example and using
inheritance, it seems that the SUPER doesn't work and that the superclass variables don't bind to the subclass.
I am using ActivePerl 5.
Any ideas and thanks,
Will



Code
################################################ 
## 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();



wtritchie
New User

Jul 1, 2010, 3:50 PM

Post #2 of 2 (578 views)
Re: [wtritchie] Getting a superclass' variables printed out [In reply to] Can't Post

This was solved in the constructor of the subclass, a line should be in there with:

Code
$self = $class->Person::new($superFirstName, $superSecondName);


The previous line was throwing the details away, it wasn't being captured in the subclass object.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives