Calling parent constructor

E

ed

Is there anything wrong with calling the parent's constructor
this way?

It all seems to work as expected. For instance if I take the
"printVars()" sub out of test3 the one in test2 executes.


# -- start perl code

{ package test1;

sub new
{
my $proto = shift;
my $class = ref($proto) || $proto;
my $this = {};
bless($this, $class);

print "in test1::new \n";

$this->{test1} = 'this is test1';

return $this;
}
}

{ package test2;

use base qw(test1);

sub new
{
$this = test1::new(@_);

print "in test2::new \n";

$this->{test2} = 'this is test2';

return $this;
}

sub printVars
{ my $this = shift;

print "in test2::printVars \n";
}
}

{ package test3;

use base qw(test2);

sub new
{
$this = test2::new(@_);

print "in test3::new\n";

$this->{test3} = 'this is test3';

return $this;
}

sub printVars
{ my $this = shift;

print "\$this->{test1} = $this->{test1} \n";
print "\$this->{test2} = $this->{test2} \n";
print "\$this->{test3} = $this->{test3} \n";
}
}

$obj = test3->new();
$obj->printVars();

#-- end perl code

tia,
--ed
 
E

ed

I believe I found out that this is the right way to do it:

$this = $this->SUPER::new(@_);

You just have to make sure you've placed the parent classes into the
@ISA array in the right order.

--ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top