in reply to Invoking method in another package
Your usage is... unusual and not good design if you're just trying to do the typical object inheritance thing. Here I've altered your code to have meaningful package names, Cow inherits from Animal and the object variables make more sense for something that might be polymorphic later (cow became sound). If you had other animals you would just put the right data into the right variables and then rooster's could cluck. I added a roster class since it was trivial once the variables were changed around. Now if you *weren't* looking for polymorphic inheritance then speak up since that's what it looks like you're getting at.
my $fb = new Cow; $fb->sound; package Rooster; use base 'Animal'; sub new { bless { animal_type => 'rooster', sound => 'cluck' }, shift } package Cow; use base 'Animal'; sub new { bless { animal_type => 'cow', sound => 'moo' }, shift } package Animal; sub sound { my $self = shift; print $self->{animal_type} . "s go: " . $self->{sound}; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Invoking method in another package
by nutshell (Beadle) on Sep 30, 2002 at 01:10 UTC | |
by diotalevi (Canon) on Sep 30, 2002 at 01:19 UTC |