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...
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.