MooseX::Traitor - An alternate way to compose your classes with traits


This document describes version 0.006 of MooseX::Traitor - released June 25, 2015 as part of MooseX-Traitor.


    # in your class definition...
    package MyClass;
    use Moose;
    use namespace::autoclean;

    with 'MooseX::Traitor';

    # somewhere else in Gotham...
    my $thinger = MyClass->with_traits('Thinger::Trait1')->new(...);


One of the most powerful things about Moose is that with roles and easy "anonymous" class creation we are blessed with a fantastic new way of creating classes, often on the fly, out of other classes and those composable bits of behaviour, roles.

Even better, this application of discrete chunks of behaviours enables people simply using a class to extend and tweak its behaviour in new ways -- possibly ways never contemplated by the authors of the classes being altered.


with_traits(<trait1>, ...)

This method builds an anonymous class from the consuming class and any traits specified.

You may use the full trait specification syntax, e.g.:

    MyClass->with_traits('My::Trait' => { -excludes => ... })

Calling this routine with no traits specified will simply return the name of the class. This is not considered an error.

Note that we handle being called directly against a package (e.g. MyClass->with_traits(...)) and against an instance (e.g. $self->with_traits(...)) identically; in each instance the class referenced is subclassed.


There are many different definitions of what a role is vs a trait, ranging from "hey man, it's all cool" to "CLOS calls them all traits SO TRAITS IS THE ONE TRUE NAME", it seems that most people tend to think of them this way:

Roles are traits that a class knowingly consumes (e.g. via with()).

Traits are roles that are applied without the class' consent (e.g. anonymous subclass composition or $trait_meta->apply('ClassThinger')).

Or maybe that's just what this author is imposing on everyone else. Either way, that's what we'll be using here if the definition ever becomes important.


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


Chris Weyl <>

I'm a material boy in a material world

Please note I do not expect to be gittip'ed or flattr'ed for this work, rather it is simply a very pleasant surprise. I largely create and release works like this because I need them or I find it enjoyable; however, don't let that stop you if you feel like it ;)

Flattr, Gratipay, or indulge my Amazon Wishlist... If and *only* if you so desire.


This software is Copyright (c) 2015 by Chris Weyl.

This is free software, licensed under:

  The GNU Lesser General Public License, Version 2.1, February 1999