MooseX::Role::TraitConstructor - A wrapper for new that can accept a traits parameter.


    package Foo;
    use Moose;

        with qw(MooseX::Role::TraitConstructor);

    package Foo::Bah;

    sub bah_method { ... }

    my $foo = Foo->new( traits => [qw( Bah )] );



This role allows you to easily accept a traits argument (or another name) into your constructor, which will easily mix roles into an anonymous class before construction, much like Moose::Meta::Attribute does.



Returns the string traits.

Override to rename the parameter.

new_with_traits %params
new_with_traits $params

A "new" in Moose::Object like parameter processor which will call new on the return value of interpolate_class_from_params.

interpolate_class_from_params $params

This method will automatically create an anonymous class with the roles from the traits param mixed into it if one exists.

If not the normal class name will be returned.

Will remove the traits parameter from $params.

Also works as an instance method, but always returns a class name.

In list context also returns the actual list of roles mixed into the class.

process_constructor_traits $params, @traits

Calls filter_constructor_traits on the result of resolve_constructor_traits.

resolve_constructor_traits $params, @traits

Attempt to load the traits specified in @traits usinc resolve_constructor_trait

guess_original_class_name $params
resolve_constructor_trait $params, $possible_root, $trait

Attempts to get a processed name from process_trait_name, and then tries to load that.

If process_trait_name didn't return a true value or its return value could not be loaded then $trait will be tried.

If nothing could be loaded an error is thrown.

$possible_root is the name of the first non anonymous class in the linearized_isa, usually $class, but will DWIM in case $class has already been interpolated with traits from a named class.

process_trait_name $trait, $params, $possible_root

Returns join "::", $possible_root, $trait.

You probably want to override this method.

filter_constructor_traits $params, $traits,

Returns all the the roles that the invocant class doesn't already do (uses does).

VERSION CONTROL Ask on #moose for commit bits.


Yuval Kogman <>


        Copyright (c) 2008 Yuval Kogman. All rights reserved
        This program is free software; you can redistribute
        it and/or modify it under the same terms as Perl itself.