Sugar for MooseX::Traits

This is a very old article. It has been imported from older blogging software, and the formatting, images, etc may have been lost. Some links may be broken. Some of the information may no longer be correct. Opinions expressed in this article may no longer be held.

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.