Thanks tobyink for this, I found it very interesting!

I learned OO on Java, then graduated to C++. So, my first thought on reading the above was: roles here are analogous to Java interfaces — except that a role contains an implementation whereas an interface does not. But then I wondered: how is this different from good old (possibly multiple) inheritance? I changed your first example by removing the references to Role::Tiny and replacing:

use Role::Tiny::With; with "Roles::Jumper";

with:

use base qw( Roles::Jumper );

in both the Pet::Dog and Pet::Cat classes. The resulting output was the same. So, my first question is: what advantage is there in using Role::Tiny over using simple, single inheritance here?

My second question arises from this statement:

When there's a clear "is-a" relationship between the classes..., and when the base class ... is potentially useful by itself, then use inheritance. Otherwise, use roles.

Can you explain the reasoning here? I’m still trying to get my head around Perl’s approach to OO. In my present state of ignorance, the two techniques you illustrate look like syntactic variations on an identical theme, and I can’t see any reason to prefer one over the other.

Thanks,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^2: OO: how to make a generic sub (use roles or inheritance) by Athanasius
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.