1Moose::Role(3) User Contributed Perl Documentation Moose::Role(3)
2
3
4
6 Moose::Role - The Moose Role
7
9 package Eq;
10 use Moose::Role; # automatically turns on strict and warnings
11
12 requires 'equal';
13
14 sub no_equal {
15 my ($self, $other) = @_;
16 !$self->equal($other);
17 }
18
19 # ... then in your classes
20
21 package Currency;
22 use Moose; # automatically turns on strict and warnings
23
24 with 'Eq';
25
26 sub equal {
27 my ($self, $other) = @_;
28 $self->as_float == $other->as_float;
29 }
30
32 The concept of roles is documented in Moose::Manual::Roles. This
33 document serves as API documentation.
34
36 Moose::Role currently supports all of the functions that Moose exports,
37 but differs slightly in how some items are handled (see "CAVEATS" below
38 for details).
39
40 Moose::Role also offers two role-specific keyword exports:
41
42 requires (@method_names)
43 Roles can require that certain methods are implemented by any class
44 which "does" the role.
45
46 Note that attribute accessors also count as methods for the
47 purposes of satisfying the requirements of a role.
48
49 excludes (@role_names)
50 Roles can "exclude" other roles, in effect saying "I can never be
51 combined with these @role_names". This is a feature which should
52 not be used lightly.
53
54 unimport
55 Moose::Role offers a way to remove the keywords it exports, through the
56 "unimport" method. You simply have to say "no Moose::Role" at the
57 bottom of your code for this to work.
58
59 Moose::Role->init_meta(for_class => $role, metaclass => $metaclass)
60 The "init_meta" method sets up the metaclass object for the role
61 specified by "for_class". It also injects a a "meta" accessor into the
62 role so you can get at this object.
63
64 The default metaclass is Moose::Meta::Role. You can specify an
65 alternate metaclass with the "metaclass" parameter.
66
68 When you use Moose::Role, you can specify which metaclass to use:
69
70 use Moose::Role -metaclass => 'My::Meta::Role';
71
72 You can also specify traits which will be applied to your role
73 metaclass:
74
75 use Moose::Role -traits => 'My::Trait';
76
77 This is very similar to the attribute traits feature. When you do this,
78 your class's "meta" object will have the specified traits applied to
79 it. See "Metaclass and Trait Name Resolution" in Moose for more
80 details.
81
83 In addition to being applied to a class using the 'with' syntax (see
84 Moose::Manual::Roles) and using the Moose::Util 'apply_all_roles'
85 method, roles may also be applied to an instance of a class using
86 Moose::Util 'apply_all_roles' or the role's metaclass:
87
88 MyApp::Test::SomeRole->meta->apply( $instance );
89
90 Doing this creates a new, mutable, anonymous subclass, applies the role
91 to that, and reblesses. In a debugger, for example, you will see class
92 names of the form " Class::MOP::Class::__ANON__::SERIAL::6 ", which
93 means that doing a 'ref' on your instance may not return what you
94 expect. See Moose::Object for 'DOES'.
95
96 Additional params may be added to the new instance by providing
97 'rebless_params'. See Moose::Meta::Role::Application::ToInstance.
98
100 Role support has only a few caveats:
101
102 · Roles cannot use the "extends" keyword; it will throw an exception
103 for now. The same is true of the "augment" and "inner" keywords
104 (not sure those really make sense for roles). All other Moose
105 keywords will be deferred so that they can be applied to the
106 consuming class.
107
108 · Role composition does its best to not be order-sensitive when it
109 comes to conflict resolution and requirements detection. However,
110 it is order-sensitive when it comes to method modifiers. All
111 before/around/after modifiers are included whenever a role is
112 composed into a class, and then applied in the order in which the
113 roles are used. This also means that there is no conflict for
114 before/around/after modifiers.
115
116 In most cases, this will be a non-issue; however, it is something
117 to keep in mind when using method modifiers in a role. You should
118 never assume any ordering.
119
121 See "BUGS" in Moose for details on reporting bugs.
122
124 Stevan Little <stevan@iinteractive.com>
125
126 Christian Hansen <chansen@cpan.org>
127
129 Copyright 2006-2010 by Infinity Interactive, Inc.
130
131 <http://www.iinteractive.com>
132
133 This library is free software; you can redistribute it and/or modify it
134 under the same terms as Perl itself.
135
136
137
138perl v5.12.2 2010-08-28 Moose::Role(3)