Mike,

Let me take a stab as I understand it.  Hopefully, some of the other Moosers
will correct me if I am wrong:

> Are traits and roles the same, here?

Yes. Metaclass traits are roles applied to the metaclass.  It IS that
simple.


The register_implementation method tells the class that a trait can be
loaded using the 'traits' meta-attribute. Some (many?) sfind it undesirable
to have to specify 'traits => [ ... ]' for each attribute.  What is more
desirable is to have that meta-attribute available to all attributes.
AFAIK, the best way to achieve this is to use the
Moose::Util::MetaRole::apply_metaroles function and installing this when
your class is used.  See my reply earlier today to Clayton for an example of
this.

This being Perl and Moose, I would not be surprised if there is a better way
to achieve this.  I think this gets to what you want, giving the trait to
every attribute, but does not do it dynamically.

What I would like to figure out myself is whether there is a way to install
a meta-attribute traits from a role.

I hope that my limited understanding helps.

Chris

Christopher Brown
Open Data
 http://www.opendatagroup.com
 http://opendatagroup.com/category/blog


On Mon, May 10, 2010 at 2:24 PM, Mike Friedman <[email protected]> wrote:

> Following up on my previous question,
>
> I'm reading about metaclass traits in the Cookbook:
>
>
> http://search.cpan.org/~flora/Moose-1.03/lib/Moose/Cookbook/Meta/Recipe5.pod<http://search.cpan.org/%7Eflora/Moose-1.03/lib/Moose/Cookbook/Meta/Recipe5.pod>
>
> My understanding was that traits were little thingies you hang off
> attributes. For example, I am using the native type attribute traits
> with great success. But the thing in this recipe appears to be nothing
> more than a role for a metaclass. Are traits and roles the same, here?
>
> Also, let's suppose I want people to say
>
>   use MyApp -traits 'HasHandlers';
>
> Do I need to provide a register_implementation method in the Moose
> namespace? What exactly does that do?
>
> Finally, is there a way I can apply the trait dynamically? (again, to
> save typing since every user of my Exporter module will want this
> trait.)
>
> Thanks for all the help so far,
>
>
> Mike
>

Reply via email to