1MooseX::Traits(3) User Contributed Perl Documentation MooseX::Traits(3)
2
3
4
6 MooseX::Traits - automatically apply roles at object creation time
7
9 Given some roles:
10
11 package Role;
12 use Moose::Role;
13 has foo => ( is => 'ro', isa => 'Int' required => 1 );
14
15 And a class:
16
17 package Class;
18 use Moose;
19 with 'MooseX::Traits';
20
21 Apply the roles to the class at "new" time:
22
23 my $class = Class->with_traits('Role')->new( foo => 42 );
24
25 Then use your customized class:
26
27 $class->isa('Class'); # true
28 $class->does('Role'); # true
29 $class->foo; # 42
30
32 Often you want to create components that can be added to a class
33 arbitrarily. This module makes it easy for the end user to use these
34 components. Instead of requiring the user to create a named class with
35 the desired roles applied, or apply roles to the instance one-by-one,
36 he can just create a new class from yours with "with_traits", and then
37 instantiate that.
38
39 There is also "new_with_traits", which exists for compatibility
40 reasons. It accepts a "traits" parameter, creates a new class with
41 those traits, and then insantiates it.
42
43 Class->new_with_traits( traits => [qw/Foo Bar/], foo => 42, bar => 1 )
44
45 returns exactly the same object as
46
47 Class->with_traits(qw/Foo Bar/)->new( foo => 42, bar => 1 )
48
49 would. But you can also store the result of "with_traits", and call
50 other methods:
51
52 my $c = Class->with_traits(qw/Foo Bar/);
53 $c->new( foo => 42 );
54 $c->whatever( foo => 1234 );
55
56 And so on.
57
59 $class->with_traits( @traits )
60 Return a new class with the traits applied. Use like:
61
62 $class->new_with_traits(%args, traits => \@traits)
63 "new_with_traits" can also take a hashref, e.g.:
64
65 my $instance = $class->new_with_traits({ traits => \@traits, foo => 'bar' });
66
68 This role will add the following attributes to the consuming class.
69
70 _trait_namespace
71 You can override the value of this attribute with "default" to
72 automatically prepend a namespace to the supplied traits. (This can be
73 overridden by prefixing the trait name with "+".)
74
75 Example:
76
77 package Another::Trait;
78 use Moose::Role;
79 has 'bar' => (
80 is => 'ro',
81 isa => 'Str',
82 required => 1,
83 );
84
85 package Another::Class;
86 use Moose;
87 with 'MooseX::Traits';
88 has '+_trait_namespace' => ( default => 'Another' );
89
90 my $instance = Another::Class->new_with_traits(
91 traits => ['Trait'], # "Another::Trait", not "Trait"
92 bar => 'bar',
93 );
94 $instance->does('Trait') # false
95 $instance->does('Another::Trait') # true
96
97 my $instance2 = Another::Class->new_with_traits(
98 traits => ['+Trait'], # "Trait", not "Another::Trait"
99 );
100 $instance2->does('Trait') # true
101 $instance2->does('Another::Trait') # false
102
104 Jonathan Rockway "<jrockway@cpan.org>"
105
106 Stevan Little "<stevan.little@iinteractive.com>"
107
108 Tomas Doran "<bobtfish@bobtfish.net>"
109
110 Matt S. Trout "<mst@shadowcatsystems.co.uk>"
111
112 Jesse Luehrs "<doy at tozt dot net>"
113
114 Shawn Moore "<sartak@bestpractical.com>"
115
116 Florian Ragwitz "<rafl@debian.org>"
117
118 Chris Prather "<chris@prather.org>"
119
120 Yuval Kogman "<nothingmuch@woobling.org>"
121
123 Copyright 2008 Infinity Interactive, Inc.
124
125 <http://www.iinteractive.com>
126
127 This library is free software; you can redistribute it and/or modify it
128 under the same terms as Perl itself.
129
130
131
132perl v5.12.0 2010-05-13 MooseX::Traits(3)