Sugar for MooseX::Traits

Tomorrow morning I'll be needing to get up in the wee small hours of the morning to travel to the Moving to Moose Hackathon 2012. In the mean time, here's some sugar for the awesome MooseX::Traits...

package traits;
use MooseX::Role::Parameterized;
parameter namespace => (
    isa     => 'Str',
    default => '',
);
role {
    with 'MooseX::Traits';
    has '_trait_namespace' => (is => 'ro', default => shift->namespace);
};
1

Instead of this:

package MyProject::MyClass;
use Moose;
with 'MooseX::Traits';
has '+_trait_namespace' => ( default => 'MyProject' );

You can now just write:

package MyProject::MyClass;
use Moose;
with traits => { namespace => 'MyProject' };

It would be better perhaps if MooseX::Traits and the 'with traits => {}' pattern could be integrated into Moose itself, avoiding the need for a top-level traits.pm module.

Leave a comment

About Toby Inkster

user-pic I'm tobyink on CPAN, IRC and PerlMonks.