Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: moose role that uses a class that does the role

by tospo (Hermit)
on Jun 28, 2012 at 13:29 UTC ( #978922=note: print w/replies, xml ) Need Help??

in reply to moose role that uses a class that does the role

I'm surprised the universe didn't implode... :-)

Never tried this but, yeah, I guess it's reasonable to assume that the short-circuit is the problem here. What is the use case for something like this? It should be possible to move common functionality out of Greeting and into the Hello role so that the Hello role implements all of that and Greeting just consumes it. After all, that's the ponit of roles.

  • Comment on Re: moose role that uses a class that does the role

Replies are listed 'Best First'.
Re^2: moose role that uses a class that does the role
by mjbetts (Initiate) on Jun 28, 2012 at 14:48 UTC
    Thanks for your reply. My use case is that I have classes that are basically lists of other objects. Within a role I have a few methods that can produce new instances of these classes. Something like the code below (the real code is a nightmare, which may be part of the problem...). There is probably a way to do this along the lines that you suggest, but I'm having a hard time figuring out how...
    package Cluster; use strict; use warnings; use Moose::Role; use namespace::autoclean; use Group; sub cluster { my($self) = @_; # cluster the members of this group, producing several # sub groups whose members may be clustered further my $sub_group1 = Group->new(); my $sub_group2 = Group->new(); return [$sub_group1, $sub_group2]; } 1;
    package Group; use strict; use warnings; use Moose; use namespace::autoclean; with 'Cluster'; __PACKAGE__->meta->make_immutable; 1;

      What if you made the role parametric in one way or another? The role could require the presence of a method (created_class_name()) which Group could provide and, in this case, returns only the string 'Group'. That would decouple the class and the role, at least in the code you've shown here.

      Another approach is to use MooseX::Role::Parameterized and pass in the class name when you compose the role.

      Improve your skills with Modern Perl: the free book.

      I guess this sort of recursion won't work as a role. A role can not depend on one of the classes it is consumed by, it should be independent and only define functionality that can be consumed by various classes. Cleary, this separation is not possible here.

      So, why a role at all? Are there really other classes that are very different from Group that can also cluster? If they are similar to the Group class then I think this would better be designed using inheritance, where Group implements the "cluster" method and then you may have sub-classes of Group (or come up with a new parent that Group is a child of too) which modify the behaviour of Group.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://978922]
erix just did a 12 km-run. Slow, but unstoppable!
[choroba]: My recent record is 10km in 1 hour :)
[perldigious]: Nice erix... perldigious' legs are so sore from portaging his kayak from place to place last weekend he can barely hobble 12m. :-)
[erix]: you're faster than me then :)
[erix]: well, at the moment, that is :)
[erix]: I should lose some weight although I keep telling myself that I'm doing extra training by lugging it along

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-05-24 12:40 GMT
Find Nodes?
    Voting Booth?