Hi Monks,
The code in question was there at node my $self is stumped!. I am pasing the code below for your reference.
How do I get the parent method to call the parent second method?
#!/usr/bin/perl
use strict;
package person;
sub new
{
my $class = shift(@_);
my $self = { 'name' => 'Alice' };
bless($self, $class);
return($self);
}
sub tellName {
my($self)=$_[0];
print "$self->{'name'}.\n";
}
sub introduce {
my($self)=$_[0];
print "Hello, I'm ";
$self->tellName();
}
1;
package child;
our @ISA = qw(person); # inherits from person
sub new {
my $class = shift(@_);
my $self = $class->SUPER::new();
$self ->{'nickName'} = 'Billy The Kid';
bless $self, $class;
return $self;
}
sub tellName {
my($self)=$_[0];
print "$self->{'nickName'}.\n";
}
sub introduce {
my($self)=$_[0]; print "Hi, I'm ";
$self->tellName();
print "Here's my mom: ";
$self->SUPER::introduce();
}
1;
package main;
my $mom = new person();
$mom->introduce(); # that time it works.
print "\n";
my $son = new child();
$son->introduce();
When the child calls the parent method with $self->Super::firstmethod(), the parent method uses $self->secondmethod().
But the secondmethod() that gets called is the over-ride version, because $self is a reference to to the child object. |