MooseX:...tructor(3)
β¨ MooseX::Role::TraitConstructor β¨
okay bestie, welcome to the User Contributed Perl Documentation corner of the internet π this is MooseX::Role::TraitConstructor, and it's basically a little wrapper for new that also lets you pass in a traits parameter. slay.
πΈ NAME πΈ
MooseX::Role::TraitConstructor β a wrapper for new that can accept a traits parameter. it's giving constructor glow-up, no cap π
π¦ SYNOPSIS π¦
package Foo;
use Moose;
with qw(MooseX::Role::TraitConstructor);
package Foo::Bah;
sub bah_method { ... }
my $foo = Foo->new( traits => [qw( Bah )] );
$foo->bah_method;
like... you with the role, you hand new a traits arrayref, and boom β bah_method just exists on your object now. main character energy fr fr β¨
π DESCRIPTION π
so this role lets you easily accept a traits argument (or literally any other name you want, we're flexible like that) into your constructor. it'll mix those roles into an anonymous class before construction happens β much like Moose::Meta::Attribute does. it's the same vibe, different department π«Ά
π METHODS π
constructor_trait_param
returns the string "traits".
override it if you wanna rename the parameter. your constructor, your rules, periodt π
new_with_traits %params
new_with_traits $params
a new in Moose::Object-like parameter processor β it calls new on whatever interpolate_class_from_params hands back. very "delegate and thrive" of it β¨
interpolate_class_from_params $params
this one will automatically create an anonymous class with the roles from the traits param mixed into it, if such a param exists. iconic behaviour honestly.
if there's no such param, the normal class name gets returned. business as usual π
it will also remove the traits parameter from $params β so heads up, it's not there anymore after this. gone. deleted. π
it also works as an instance method, but it always returns a class name. no exceptions bestie.
and in list context it also returns the actual list of roles that got mixed into the class. two-for-one, we love to see it π
process_constructor_traits $params, @traits
calls filter_constructor_traits on the result of resolve_constructor_traits. chain reaction, very domino effect β
resolve_constructor_traits $params, @traits
attempts to load the traits specified in @traits, usinc resolve_constructor_trait. (yes bestie that says usinc, we keep it exactly as the ancestors wrote it π)
guess_original_class_name $params
that's it. that's the method. it's in the list, it's doing its thing π§
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 itself will be tried.
and if nothing could be loaded, an error is thrown. so like, if you get here with nothing, that's a yikes π
$possible_root is the name of the first non-anonymous class in the linearized_isa. usually that's $class, but it'll DWIM in case $class has already been interpolated with traits from a named class. it's smart like that π
process_trait_name $trait, $params, $possible_root
returns join "::", $possible_root, $trait.
and yeah β you probably want to override this method. it's basically begging you to make it yours π
filter_constructor_traits $params, $traits,
returns all the roles that the invocant class doesn't already do β it uses does to check. no duplicate role energy allowed in this house π«β¨
π VERSION CONTROL π
<http://code2.0beta.co.uk/moose/svn/>. ask on #moose for commit bits. iykyk π
π AUTHOR π
Yuval Kogman <nothingmuch@woobling.org>
β COPYRIGHT β
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.
and that's the whole page, bestie π«Ά this was perl v5.42.0, dated 2025-07-25, and it ate. no notes. go mix some traits into an anonymous class and be free πΈππ
π¦ where this page comes from
- fedora 43: perl-MooseX-Role-TraitConstructor 0.01-38.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms