Tuesday July 15, 2008
05:26 PM
Thoughts on parametrized roles for Moose
Moose might allow roles a little bit more freedom on how the are used by allowing parameters to be passed to roles. So the role can use those during initialization to act accordingly.
This will allow to e.g. set a method prefix for a role's methods, or set some defaults, initialize some other stuff...
Here's how it could look like:
use Moose;
with role 'My::Role'; ### no magic here same as: with 'My::Role';
with role 'MooseX::Log::Log4perl' => ':easy'; ### pass a param
with role 'MooseX::Log::Log4perl', prefix => 'mylog_'; ### pass the param hash/pair to a role
For more on that read this post...
Some of this already (kinda) exists ... (Score:1)
So we already have a mechanism in place whereby extra parameters can be passed into a role during composition, see Recipie 11 [cpan.org] for an example of the features that utilize this. Basically anything that is not another string passed after the roles gets turned into params (rjbs++ for Data::OptList which basically does all that work for me). So your examples would basically turn into this: