in reply to About inheritence ? and Autoload ?
About namespaces and inheritance:
Namespaces, like "Animal::Dog", are just names, as was written before. Inheritance is defined differently, but it is good habit to create namespaces related with inheritance, for instance:
Now you can create some instance of animal :>) by this way:package Animal; #parent class, in file lib/Animal.pm sub new { return bless {}, shift; } sub sound { die "cannot call this method on class '".ref(shift)."'"; } sub output_sound { my ($self, $what) = @_; print $what."\n"; } 1; package Animal::Dog; #desc. of Animal, lib/Animal/Dog.pm use base 'Animal'; sub sound { my ($self) = @_; $self->output_sound("haf"); } 1; package Animal::Dog::Sheepdog; #desc. of Dog #lib/Animal/Dog/Sheepdog.pm use base 'Animal::Dog'; sub guard { my ($self, $flock) = @_; #do something } 1; package Animal::Pig; #other descendant use base 'Animal'; sub sound { my ($self) = @_; $self->output_sound("khro"); } 1;
Do not hesitate to ask some questions, I had been confused in the same manner. Just note: that code is not tested, it really should have strictings in every module...use Animal::Dog::Sheepdog; my $helper = Animal::Dog::Sheepdog->new(); #Once you have an instance, you can call it's methods by this way: $helper->sound;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: About inheritence ? and Autoload ?
by lepetitalbert (Abbot) on Nov 25, 2009 at 11:18 UTC | |
by pajout (Curate) on Nov 25, 2009 at 12:11 UTC | |
by kyle (Abbot) on Nov 25, 2009 at 13:24 UTC | |
by lepetitalbert (Abbot) on Nov 25, 2009 at 16:36 UTC |
In Section
Seekers of Perl Wisdom