1MooseX::Role::ParameterUiszeerd(C3o)ntributed Perl DocumMeonotsaetXi:o:nRole::Parameterized(3)
2
3
4
6 MooseX::Role::Parameterized - Moose roles with composition parameters
7
9 version 1.10
10
12 package Counter;
13 use MooseX::Role::Parameterized;
14
15 parameter name => (
16 isa => 'Str',
17 required => 1,
18 );
19
20 role {
21 my $p = shift;
22
23 my $name = $p->name;
24
25 has $name => (
26 is => 'rw',
27 isa => 'Int',
28 default => 0,
29 );
30
31 method "increment_$name" => sub {
32 my $self = shift;
33 $self->$name($self->$name + 1);
34 };
35
36 method "reset_$name" => sub {
37 my $self = shift;
38 $self->$name(0);
39 };
40 };
41
42 package MyGame::Weapon;
43 use Moose;
44
45 with Counter => { name => 'enchantment' };
46
47 package MyGame::Wand;
48 use Moose;
49
50 with Counter => { name => 'zapped' };
51
53 Your parameterized role consists of two new things: parameter
54 declarations and a "role" block.
55
56 Parameters are declared using the "parameter" keyword which very much
57 resembles "has" in Moose. You can use any option that "has" in Moose
58 accepts. The default value for the "is" option is "ro" as that's a very
59 common case. Use "is => 'bare'" if you want no accessor. These
60 parameters will get their values when the consuming class (or role)
61 uses "with" in Moose. A parameter object will be constructed with these
62 values, and passed to the "role" block.
63
64 The "role" block then uses the usual Moose::Role keywords to build up a
65 role. You can shift off the parameter object to inspect what the
66 consuming class provided as parameters. You use the parameters to
67 customize your role however you wish.
68
69 There are many possible implementations for parameterized roles
70 (hopefully with a consistent enough API); I believe this to be the
71 easiest and most flexible design. Coincidentally, Pugs originally had
72 an eerily similar design.
73
74 See MooseX::Role::Parameterized::Extending for some tips on how to
75 extend this module.
76
77 Why a parameters object?
78 I've been asked several times "Why use a parameter object and not just
79 a parameter hashref? That would eliminate the need to explicitly
80 declare your parameters."
81
82 The benefits of using an object are similar to the benefits of using
83 Moose. You get an easy way to specify lazy defaults, type constraint,
84 delegation, and so on. You get to use MooseX modules.
85
87 Stop! If you're new here, please read
88 MooseX::Role::Parameterized::Tutorial for a much gentler introduction.
89
90 You also get the usual introspective and intercessory abilities that
91 come standard with the metaobject protocol. Ambitious users should be
92 able to add traits to the parameters metaclass to further customize
93 behavior. Please let me know if you're doing anything viciously
94 complicated with this extension. :)
95
97 You must use this syntax to declare methods in the role block: "method
98 NAME => sub { ... };". This is due to a limitation in Perl. In return
99 though you can use parameters in your methods!
100
102 <http://sartak.org/2009/01/parametric-roles-in-perl-5.html>
103
104 <http://sartak.org/2009/05/the-design-of-parameterized-roles.html>
105
106 <http://stevan-little.blogspot.com/2009/07/thoughts-on-parameterized-roles.html>
107
108 <http://perldition.org/articles/Parameterized%20Roles%20with%20MooseX::Declare.pod>
109
110 <http://www.modernperlbooks.com/mt/2011/01/the-parametric-role-of-my-mvc-plugin-system.html>
111
112 <http://jjnapiorkowski.typepad.com/modern-perl/2010/08/parameterized-roles-and-method-traits-redo.html>
113
114 <http://sartak.org/talks/yapc-asia-2009/(parameterized)-roles/>
115
116 <https://github.com/SamuraiJack/JooseX-Role-Parameterized> - this
117 extension ported to JavaScript's Joose
118
120 Bugs may be submitted through the RT bug tracker
121 <https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Role-
122 Parameterized> (or bug-MooseX-Role-Parameterized@rt.cpan.org
123 <mailto:bug-MooseX-Role-Parameterized@rt.cpan.org>).
124
125 There is also a mailing list available for users of this distribution,
126 at <http://lists.perl.org/list/moose.html>.
127
128 There is also an irc channel available for users of this distribution,
129 at "#moose" on "irc.perl.org" <irc://irc.perl.org/#moose>.
130
132 Shawn M Moore <code@sartak.org>
133
135 · Karen Etheridge <ether@cpan.org>
136
137 · Dave Rolsky <autarch@urth.org>
138
139 · Jesse Luehrs <doy@tozt.net>
140
141 · Oliver Charles <oliver.g.charles@googlemail.com>
142
143 · Yuval Kogman <nothingmuch@woobling.org>
144
145 · Robert 'phaylon' Sedlacek <rs@474.at>
146
147 · Florian Ragwitz <rafl@debian.org>
148
149 · Mark Fowler <mark@twoshortplanks.com>
150
151 · Chris Weyl <cweyl@alumni.drew.edu>
152
153 · Csson <erik.carlsson@live.com>
154
155 · Andy Jack <github@veracity.ca>
156
157 · Ricardo Signes <rjbs@cpan.org>
158
159 · Todd Hepler <thepler@employees.org>
160
162 This software is copyright (c) 2008 by Shawn M Moore.
163
164 This is free software; you can redistribute it and/or modify it under
165 the same terms as the Perl 5 programming language system itself.
166
167
168
169perl v5.28.0 2017-04-20 MooseX::Role::Parameterized(3)