MooseX::Traits(3pm)

πŸ“¦
version 0.13 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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_traits can 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>

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