in reply to Re^3: OO: how to make a generic sub (use roles or inheritance)
in thread OO: how to make a generic sub

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,

Replies are listed 'Best First'.
Re^5: OO: how to make a generic sub (use roles or inheritance)
by tobyink (Canon) on Jun 27, 2013 at 14:01 UTC

    This appears to be a bug. Your example works perfectly switching from Role::Tiny to Moose::Role. I'll ask around and update this node with what I find out.

    Update: yes, it's a massive bug with a one line fix (an eight character fix, actually). I'm shocked and appalled that such a clear problem exists in Role-Tiny. I've pushed a test case and fix to the Role-Tiny repository, but don't have the necessary PAUSE permissions to release a quick fix to CPAN. I'll try to badger somebody who does into getting it released quickly.

    Update (2.5 weeks later): Role::Tiny 1.003000 is out and fixes this issue.

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