in reply to Re: SUPER in OOPerl is dumped with $self
in thread SUPER in OOPerl is dumped with $self
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: SUPER in OOPerl is dumped with $self
by pajout (Curate) on Jul 03, 2007 at 10:47 UTC | |
by atemon (Chaplain) on Jul 03, 2007 at 12:42 UTC | |
by pajout (Curate) on Jul 03, 2007 at 14:24 UTC | |
by dsheroh (Monsignor) on Jul 03, 2007 at 16:31 UTC | |
by dsheroh (Monsignor) on Jul 03, 2007 at 20:10 UTC | |
|
Re^3: SUPER in OOPerl is dumped with $self
by dsheroh (Monsignor) on Jul 03, 2007 at 16:24 UTC |