1MooseX::Role::ParameterUiszeerd(C3o)ntributed Perl DocumMeonotsaetXi:o:nRole::Parameterized(3)
2
3
4
6 MooseX::Role::Parameterized - roles with composition parameters
7
9 package Counter;
10 use MooseX::Role::Parameterized;
11
12 parameter name => (
13 isa => 'Str',
14 required => 1,
15 );
16
17 role {
18 my $p = shift;
19
20 my $name = $p->name;
21
22 has $name => (
23 is => 'rw',
24 isa => 'Int',
25 default => 0,
26 );
27
28 method "increment_$name" => sub {
29 my $self = shift;
30 $self->$name($self->$name + 1);
31 };
32
33 method "reset_$name" => sub {
34 my $self = shift;
35 $self->$name(0);
36 };
37 };
38
39 package MyGame::Weapon;
40 use Moose;
41
42 with Counter => { name => 'enchantment' };
43
44 package MyGame::Wand;
45 use Moose;
46
47 with Counter => { name => 'zapped' };
48
50 Stop! If you're new here, please read
51 MooseX::Role::Parameterized::Tutorial for a much gentler introduction.
52
54 Your parameterized role consists of two new things: parameter
55 declarations and a "role" block.
56
57 Parameters are declared using the "parameter" keyword which very much
58 resembles "has" in Moose. You can use any option that "has" in Moose
59 accepts. The default value for the "is" option is "ro" as that's a very
60 common case. Use "is => 'bare'" if you want no accessor. These
61 parameters will get their values when the consuming class (or role)
62 uses "with" in Moose. A parameter object will be constructed with these
63 values, and passed to the "role" block.
64
65 The "role" block then uses the usual Moose::Role keywords to build up a
66 role. You can shift off the parameter object to inspect what the
67 consuming class provided as parameters. You use the parameters to
68 customize your role however you wish.
69
70 There are many possible implementations for parameterized roles
71 (hopefully with a consistent enough API); I believe this to be the
72 easiest and most flexible design. Coincidentally, Pugs originally had
73 an eerily similar design.
74
75 See MooseX::Role::Parameterized::Extending for some tips on how to
76 extend this module.
77
78 Why a parameters object?
79 I've been asked several times "Why use a parameter object and not just
80 a parameter hashref? That would eliminate the need to explicitly
81 declare your parameters."
82
83 The benefits of using an object are similar to the benefits of using
84 Moose. You get an easy way to specify lazy defaults, type constraint,
85 delegation, and so on. You get to use MooseX modules.
86
87 You also get the usual introspective and intercessory abilities that
88 come standard with the metaobject protocol. Ambitious users should be
89 able to add traits to the parameters metaclass to further customize
90 behavior. Please let me know if you're doing anything viciously
91 complicated with this extension. :)
92
94 You must use this syntax to declare methods in the role block: "method
95 NAME => sub { ... };". This is due to a limitation in Perl. In return
96 though you can use parameters in your methods!
97
98 "alias" in Moose::Role and "excludes" in Moose::Role are not yet
99 supported. I'm completely unsure of whether they should be handled by
100 this module. Until we figure out a plan, either declaring or providing
101 a parameter named "alias" or "excludes" is an error.
102
104 Shawn M Moore, "sartak@gmail.com"
105
107 Fey::Role::HasAliasName
108 Fey::Role::MakesAliasObjects
109 Fey::Role::SQL::Cloneable
110 Fey::Role::SetOperation
111 IM::Engine::PluggableConstructor
112 IM::Engine::RequiresPlugins
113 KiokuDB::Role::Scan
114 MooseX::RelatedClassRoles
115 MooseX::Role::Matcher
116 MooseX::Role::XMLRPC::Client
117 MooseX::WithCache
118 Net::Journyx::Object::Loadable
119 NetHack::Item::Role::IncorporatesStats
120 TAEB::Action::Role::Item
121 WWW::Mechanize::TreeBuilder
122
124 http://sartak.blogspot.com/2009/05/parameterized-roles.html
125 <http://sartak.blogspot.com/2009/05/parameterized-roles.html>
126
127 http://stevan-little.blogspot.com/2009/07/thoughts-on-parameterized-roles.html
128 <http://stevan-little.blogspot.com/2009/07/thoughts-on-parameterized-
129 roles.html>
130
131 http://sartak.org/talks/yapc-asia-2009/(parameterized)-roles/
132 <http://sartak.org/talks/yapc-asia-2009/(parameterized)-roles/>
133
135 Copyright 2007-2009 Infinity Interactive
136
137 This program is free software; you can redistribute it and/or modify it
138 under the same terms as Perl itself.
139
140
141
142perl v5.12.0 2010-03-10 MooseX::Role::Parameterized(3)