MooseX::Traits(3pm)
π MooseX::Traits π
okay bestie, welcome to MooseX::Traits(3pm) β that's the User Contributed Perl Documentation shelf, and this lil page is version 0.13 of the module. it's the one that just automatically applies roles at object creation time. no cap. you don't have to hand-build classes anymore, the module does the glow-up for you β¨
πΈ NAME πΈ
MooseX::Traits β automatically apply roles at object creation time. that's it, that's the whole personality π
π¦ VERSION π¦
version 0.13
β¨ SYNOPSIS β¨
so picture this: you got some roles, right? here's one:
package Role;
use Moose::Role;
has foo => ( is => 'ro', isa => 'Int' required => 1 );
and here's a class:
package Class;
use Moose;
with 'MooseX::Traits';
now the fun part β you apply those roles to the class at new time, like:
my $class = Class->with_traits('Role')->new( foo => 42 );
and then you just⦠use your customized class. iconic behaviour:
$class->isa('Class'); # true
$class->does('Role'); # true
$class->foo; # 42
periodt. π
π DESCRIPTION π
okay so here's the whole vibe. a lot of the time you wanna make components that can get added onto a class arbitrarily. this module makes that super easy for whoever's using your stuff. instead of making them sit there and write out a whole named class with the roles they want baked in, or apply roles to the instance one at a time like a peasant π β they can just call with_traits on your class, get a brand new class, and instantiate that. main character energy fr fr.
there's also new_with_traits, which is honestly just there for compatibility reasons. it takes a traits parameter, makes a new class with those traits, and instantiates it.
Class->new_with_traits( traits => [qw/Foo Bar/], foo => 42, bar => 1 )
returns exactly the same object as
Class->with_traits(qw/Foo Bar/)->new( foo => 42, bar => 1 )
would. literally the same. but plot twist β you can also just hold onto the result of with_traits and call other methods on it:
my $c = Class->with_traits(qw/Foo Bar/);
$c->new( foo => 42 );
$c->whatever( foo => 1234 );
and so on and so on, the possibilities are endless bestie π
π METHODS π
$class->with_traits( @traits )β returns a brand new class with the traits applied. use it like the stuff we showed above.$class->new_with_traits(%args, traits => \@traits)βnew_with_traitscan also take a hashref instead, like this:
my $instance = $class->new_with_traits({ traits => \@traits, foo => 'bar' });
π ATTRIBUTES YOUR CLASS GETS π
this role is gonna hand your consuming class the following attributes. freebies! π«Ά
_trait_namespace
you can override the value of this attribute using default so it automatically prepends a namespace to whatever traits you supply. (heads up though β this can be overridden by prefixing the trait name with "+".)
example time:
package Another::Trait;
use Moose::Role;
has 'bar' => (
is => 'ro',
isa => 'Str',
required => 1,
);
package Another::Class;
use Moose;
with 'MooseX::Traits';
has '+_trait_namespace' => ( default => 'Another' );
my $instance = Another::Class->new_with_traits(
traits => ['Trait'], # "Another::Trait", not "Trait"
bar => 'bar',
);
$instance->does('Trait') # false
$instance->does('Another::Trait') # true
my $instance2 = Another::Class->new_with_traits(
traits => ['+Trait'], # "Trait", not "Another::Trait"
);
$instance2->does('Trait') # true
$instance2->does('Another::Trait') # false
see how the + flips it? sneaky but slay π₯
π AUTHOR π
Jonathan Rockway <jrockway@cpan.org>
π CONTRIBUTORS π
- Karen Etheridge
<ether@cpan.org> - Florian Ragwitz
<rafl@debian.org> - Tomas Doran
<bobtfish@bobtfish.net> - Hans Dieter Pearcey
<hdp@weftsoar.net> - Rafael Kitover
<rkitover@cpan.org> - Stevan Little
<stevan.little@iinteractive.com> - Alexander Hartmaier
<abraxxa@cpan.org>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2008 by Infinity Interactive, Inc. http://www.iinteractive.com.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page bestie, tysm for reading π₯Ήπ this one was generated by perl v5.34.0 on 2022-06-15 β go forth and with_traits responsibly, ily, bye πβ¨
π¦ where this page comes from
- debian bookworm: libmoosex-traits-perl 0.13-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmoosex-traits-perl 0.13-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmoosex-traits-perl 0.13-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmoosex-traits-perl 0.13-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoosex-traits-perl 0.13-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms