Consider following example:
test.pl:
#!/usr/bin/perl
use Data::Dumper;
use Animal::Dog;
my $rex = Animal::Dog->new();
print Dumper($rex);
Aninal.pm:
package Animal;
use strict;
use warnings;
sub new {
my ($class) = @_;
my $proto = {need_oxygen => 1};
return bless($proto, $class);
}
1;
Animal/Dog.pm:
package Animal::Dog;
use base "Animal";
use strict;
use warnings;
sub new {
my ($class) = @_;
my $self = $class->SUPER::new();
$$self{domesticated} = 1;
return $self;
}
1;
Running test.pl will return:
$VAR1 = bless( {
'domesticated' => 1,
'need_oxygen' => 1
}, 'Animal::Dog' );
When parent method (Animal::new in the example) does something important (it sets 'need_oxygen' for all animals), you can call it WITHOUT explicit hardcoding of the parent class, as was already noticed.
For instance, in Animal::Dog::Husky->some_method body, you can call some inherited method and you do not care if that method is written in Animal or in Animal::Dog class.
UPDATE: s/inherited/overiden/
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
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.