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...
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|