tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:
The code above contains a animal class and a horse class. The ISA-array should as I understand it instruct the hosre class to inherit from the animalclass. Thus the horse should be an animal and be able to use animal methods. When I run the code however the result is:#!/user/bin/perl -w use strict; my $horse1 = Horse->new("George the Horse", 12); $horse1->present(); $horse1->animalmethod(); ############################ ############################ package Animal; { sub animalmethod { my ($self)=@_; print "I´m an animal. My name is $self->{Name}. I´m $self->{Age} y +ears old\n"; } } package Horse; { our @ISA=("Animal"); sub new { my ($class, $name, $age) = @_; my $ref = {Name=>$name, Age=>$age}; bless($ref, $class); } sub present { my ($self)=@_; if($self->isa("Animal")) { print "I'm an animal.\n"; } else { print "I'm not an animal.\n"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inheritance in Perl5
by chromatic (Archbishop) on Aug 08, 2009 at 18:12 UTC | |
|
Re: Inheritance in Perl5
by FunkyMonk (Bishop) on Aug 08, 2009 at 13:40 UTC | |
by chromatic (Archbishop) on Aug 08, 2009 at 18:17 UTC | |
by EvanCarroll (Chaplain) on Aug 08, 2009 at 17:46 UTC | |
|
Re: Inheritance in Perl5
by Bloodnok (Vicar) on Aug 08, 2009 at 13:45 UTC | |
|
Re: Inheritance in Perl5
by ikegami (Patriarch) on Aug 08, 2009 at 15:30 UTC |