1MooseX::Traits(3pm)   User Contributed Perl Documentation  MooseX::Traits(3pm)
2
3
4

NAME

6       MooseX::Traits - Automatically apply roles at object creation time
7

VERSION

9       version 0.13
10

SYNOPSIS

12       Given some roles:
13
14         package Role;
15         use Moose::Role;
16         has foo => ( is => 'ro', isa => 'Int' required => 1 );
17
18       And a class:
19
20         package Class;
21         use Moose;
22         with 'MooseX::Traits';
23
24       Apply the roles to the class at "new" time:
25
26         my $class = Class->with_traits('Role')->new( foo => 42 );
27
28       Then use your customized class:
29
30         $class->isa('Class'); # true
31         $class->does('Role'); # true
32         $class->foo; # 42
33

DESCRIPTION

35       Often you want to create components that can be added to a class
36       arbitrarily.  This module makes it easy for the end user to use these
37       components.  Instead of requiring the user to create a named class with
38       the desired roles applied, or apply roles to the instance one-by-one,
39       he can just create a new class from yours with "with_traits", and then
40       instantiate that.
41
42       There is also "new_with_traits", which exists for compatibility
43       reasons.  It accepts a "traits" parameter, creates a new class with
44       those traits, and then instantiates it.
45
46          Class->new_with_traits( traits => [qw/Foo Bar/], foo => 42, bar => 1 )
47
48       returns exactly the same object as
49
50          Class->with_traits(qw/Foo Bar/)->new( foo => 42, bar => 1 )
51
52       would.  But you can also store the result of "with_traits", and call
53       other methods:
54
55          my $c = Class->with_traits(qw/Foo Bar/);
56          $c->new( foo => 42 );
57          $c->whatever( foo => 1234 );
58
59       And so on.
60

METHODS

62       $class->with_traits( @traits )
63           Return a new class with the traits applied.  Use like:
64
65       $class->new_with_traits(%args, traits => \@traits)
66           "new_with_traits" can also take a hashref, e.g.:
67
68             my $instance = $class->new_with_traits({ traits => \@traits, foo => 'bar' });
69

ATTRIBUTES YOUR CLASS GETS

71       This role will add the following attributes to the consuming class.
72
73   _trait_namespace
74       You can override the value of this attribute with "default" to
75       automatically prepend a namespace to the supplied traits.  (This can be
76       overridden by prefixing the trait name with "+".)
77
78       Example:
79
80         package Another::Trait;
81         use Moose::Role;
82         has 'bar' => (
83             is       => 'ro',
84             isa      => 'Str',
85             required => 1,
86         );
87
88         package Another::Class;
89         use Moose;
90         with 'MooseX::Traits';
91         has '+_trait_namespace' => ( default => 'Another' );
92
93         my $instance = Another::Class->new_with_traits(
94             traits => ['Trait'], # "Another::Trait", not "Trait"
95             bar    => 'bar',
96         );
97         $instance->does('Trait')          # false
98         $instance->does('Another::Trait') # true
99
100         my $instance2 = Another::Class->new_with_traits(
101             traits => ['+Trait'], # "Trait", not "Another::Trait"
102         );
103         $instance2->does('Trait')          # true
104         $instance2->does('Another::Trait') # false
105

AUTHOR

107       Jonathan Rockway <jrockway@cpan.org>
108

CONTRIBUTORS

110       ·   Karen Etheridge <ether@cpan.org>
111
112       ·   Florian Ragwitz <rafl@debian.org>
113
114       ·   Tomas Doran <bobtfish@bobtfish.net>
115
116       ·   Hans Dieter Pearcey <hdp@weftsoar.net>
117
118       ·   Rafael Kitover <rkitover@cpan.org>
119
120       ·   Stevan Little <stevan.little@iinteractive.com>
121
122       ·   Alexander Hartmaier <abraxxa@cpan.org>
123
125       This software is copyright (c) 2008 by Infinity Interactive, Inc.
126       http://www.iinteractive.com.
127
128       This is free software; you can redistribute it and/or modify it under
129       the same terms as the Perl 5 programming language system itself.
130
131
132
133perl v5.32.0                      2020-07-28               MooseX::Traits(3pm)
Impressum