Thanks for the excellent explanation.

I now see the problem with multiple inheritance. If my FragileDancer class has use base qw( Fragile Dancer ); then a call to break() produces damage; but if the class has use base qw( Dancer Fragile ); then break() produces dancing. And no warning that a method name conflict has been encountered.

So, I can see how roles could be useful here. But my experiments with Role::Tiny have so far been less than encouraging. :-( My first thought was:

package FragileDancer { use Role::Tiny::With; with "Fragile"; with "Dancer"; ...

but that turns out to be a documented way to resolve the conflict — it silently gives Fragile behaviour to break() because the Fragile role is listed first. So, it’s no different to the scenario with multiple inheritance. To get an exception, it seems I have to write:

package FragileDancer { use Role::Tiny::With; with 'Fragile', 'Dancer'; ...

which produces:

Due to a method name conflict between roles 'Dancer and Fragile', the +method 'break' must be implemented by 'FragileDancer' at C:\Perl\lib\ +perl5/Role/Tiny.pm line 183.

So far, so good. But when I implement that FragileDancer::break method as required, it makes no difference: the exception is generated exactly as before.

Here is my code:

#! perl use strict; use warnings; package Fragile { use Role::Tiny; sub break { print shift->{name}, " is being damaged!\n"; } } package Dancer { use Role::Tiny; sub break { print shift->{name}, " is break dancing!\n"; } } package FragileDancer { use Role::Tiny::With; with 'Fragile', 'Dancer'; sub break { print 'For ', shift->{name}, " FragileDancer::break\n" +; } sub new { my ($class, $name) = @_; my $self = { name => $name }; return bless $self, $class; } } my $fg = FragileDancer->new('Nina'); $fg->break();

Output:

23:44 >perl 655_SoPW.pl Due to a method name conflict between roles 'Dancer and Fragile', the +method 'break' must be implemented by 'FragileDancer' at C:\Perl\lib\ +perl5/Role/Tiny.pm line 183. 23:44 >

I also tried splitting the packages into separate files (i.e., giving each package its own .pm module), but that made no difference. I’m running Strawberry Perl v5.16.0 on 32-bit Vista, with Role::Tiny 1.002005.

What am I missing here?

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


In reply to Re^4: 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.