MooseX:...tructor(3)

πŸ“¦
version 0.01 Β· 2 versions
, in fedora 43

✨ 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 (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