There are two different ways of doing it. Let's assume you have a Pet::Dog class and a Pet::Cat class, and you want both animals to be able to jump.
The first technique is to use roles. A role is a unit of functionality that can be imported into several different classes. There are various packages on CPAN for creating roles, but here we'll use Role::Tiny...
#!/usr/bin/env perl use v5.14; package Roles::Jumper { use Role::Tiny; # The 'jump' method calls 'name', which means that all # classes that consume this role, must provide a 'name' # method. # requires "name"; # And here's the 'jump' method itself. # sub jump { my $self = shift; say $self->name, " jumps!"; } } package Pet::Dog { # The Pet::Dog class should consume Roles::Jumper. # use Role::Tiny::With; with "Roles::Jumper"; sub new { my $class = shift; bless {@_}, $class; } sub name { my $self = shift; return $self->{name}; } sub sound { my $self = shift; say $self->name, " says woof!"; } } package Pet::Cat { # The Pet::Cat class should also consume Roles::Jumper. # use Role::Tiny::With; with "Roles::Jumper"; sub new { my $class = shift; bless {@_}, $class; } sub name { my $self = shift; return $self->{name}; } sub sound { my $self = shift; say $self->name, " says meow!"; } } my $fido = Pet::Dog->new(name => "Fido"); $fido->jump; $fido->sound; my $felix = Pet::Cat->new(name => "Felix"); $felix->jump; $felix->sound;
The other technique is to use inheritance from a base class. A Pet::Cat is a Pet::Mammal, isn't it? And so is a Pet::Dog. So we create a Pet::Mammal class and put all our common functionality into there. Then Pet::Cat and Pet::Dog can be simple subclasses of Pet::Mammal.
#!/usr/bin/env perl use v5.14; package Pet::Mammal { sub new { my $class = shift; bless {@_}, $class; } sub name { my $self = shift; return $self->{name}; } sub jump { my $self = shift; say $self->name, " jumps!"; } } package Pet::Dog { use base "Pet::Mammal"; sub sound { my $self = shift; say $self->name, " says woof!"; } } package Pet::Cat { use base "Pet::Mammal"; sub sound { my $self = shift; say $self->name, " says meow!"; } } my $fido = Pet::Dog->new(name => "Fido"); $fido->jump; $fido->sound; my $felix = Pet::Cat->new(name => "Felix"); $felix->jump; $felix->sound;
So which technique should you use? When there's a clear "is-a" relationship between the classes (a Cat is-a Mammal, a Dog is-a Mammal), and when the base class (Mammal in this case) is potentially useful by itself, then use inheritance. Otherwise, use roles.
Update: I'll also point out that it often makes sense to use both inheritance and roles. For example a "Guard::Dog" class could inherit from "Pet::Dog" and also compose the "Security::Guard" role.
package Security::Guard { use Role::Tiny; requires "sound"; sub raise_alarm { my $self = shift; $self->sound; } } package Guard::Dog { use base "Pet::Dog"; use Role::Tiny::With; with "Security::Guard"; }
Update II: also note that while I've used syntax from Perl 5.14 in the examples above, this is merely for clarity. Role::Tiny and base each support Perl 5.8, and the examples could be rewritten to run on Perl 5.8 with fairly trivial changes.
In reply to Re: OO: how to make a generic sub (use roles or inheritance)
by tobyink
in thread OO: how to make a generic sub
by Hossein
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |