Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

by mjbetts (Initiate)
on Jun 28, 2012 at 14:48 UTC ( #978952=note: print w/replies, xml ) Need Help??

in reply to Re: moose role that uses a class that does the role
in thread moose role that uses a class that does the role

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;

Replies are listed 'Best First'.
Re^3: moose role that uses a class that does the role
by chromatic (Archbishop) on Jun 29, 2012 at 01:45 UTC

    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.

Re^3: moose role that uses a class that does the role
by tospo (Hermit) on Jun 28, 2012 at 15:15 UTC

    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://978952]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-03-18 14:40 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (230 votes). Check out past polls.