http://qs321.pair.com?node_id=809292


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:

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;
Now you can create some instance of animal :>) by this way:
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;
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...