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.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.