Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Applying roles on the client-side (per-object role composition)

by perlancar (Hermit)
on May 22, 2020 at 01:08 UTC ( [id://11117073]=perlquestion: print w/replies, xml ) Need Help??

perlancar has asked for the wisdom of the Perl Monks concerning the following question:

How do you do per-object role composition on the typical OO/role frameworks e.g. Moose or Moo (I'm using Role::Tiny in this particular case)? So basically I create a role for a class, and I want the user to be able to mix that class on an as-needed basis. The manual way would be for the user to create a subclass:

package MyClass_With_Role1; use parent 'MyClass'; use Role::Tiny::With; with 'Role1';

Is there a shortcut for it? Something like:

my $obj = MyClass->new_with_roles('Role1');

I suspect Moose et al supports something like this, but I've never done this and do not know where to look.

Replies are listed 'Best First'.
Re: Applying roles on the client-side (per-object role composition)
by Anonymous Monk on May 22, 2020 at 03:15 UTC
    Role::Tiny says
    apply_roles_to_object Role::Tiny->apply_roles_to_object($foo, qw(Some::Role1 Some::Role2) +);
      Perfect, thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11117073]
Approved by Fletch
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-19 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found