1MooX::Role::ParameterizUesde(r3)Contributed Perl DocumenMtoaotXi:o:nRole::Parameterized(3)
2
3
4
6 MooX::Role::Parameterized - roles with composition parameters
7
9 package My::Role;
10
11 use Moo::Role;
12 use MooX::Role::Parameterized;
13
14 role {
15 my $params = shift;
16 my $mop = shift;
17
18 $mop->has( $params->{attr} => ( is => 'rw' ));
19
20 $mop->method($params->{method} => sub {
21 1024;
22 });
23 };
24
25 package My::Class;
26
27 use Moo;
28 # experimental way of add roles
29 use MooX::Role::Parameterized::With My::Role => {
30 attr => 'baz',
31 method => 'run'
32 };
33
34 package My::OldClass;
35
36 use Moo;
37 use My::Role;
38
39 My::Role->apply([{ # original way of add this role
40 attr => 'baz', # add attribute read-write called 'baz'
41 method => 'run' # add method called 'run' and return 1024
42 }
43 , # and if the apply receives one arrayref
44 { attr => 'bam', # will call the role block multiple times.
45 method => 'jump' # PLEASE CALL apply once
46 }]);
47
49 It is an experimental port of MooseX::Role::Parameterized to Moo.
50
52 This package exports four subroutines: "role", "apply", "hasp" and
53 "method". The last two are now consider deprecated and will be removed
54 soon.
55
56 role
57 This function accepts just one code block. Will execute this code then
58 we apply the Role in the target class, and will receive the parameter
59 list + one mop object.
60
61 The mop object is a proxy to the target class. It offer a better way to
62 call "has", "requires" or "after" without side effects.
63
64 The old way to create parameterized roles was calling "has" or
65 "method", but there is too much problems with this approach. To solve
66 part of them we add the hasp but it solve part of the problem. To be
67 clean, we decide be explicit and offer one object with full Role
68 capability.
69
70 Instead do
71
72 my ($p) = @_;
73 ...
74 hasp $p->{attribute} => (...);
75
76 We prefer
77
78 my ($p, $mop) = @_;
79 ...
80 $mop->has($p->{attribute} =>(...));
81
82 Less magic, less problems.
83
84 apply
85 When called, will apply the "role" on the current package. The behavior
86 depends of the parameter list.
87
88 This will install the role in the target package. Does not need call
89 "with".
90
91 Important, if you want to apply the role multiple times, like to create
92 multiple attributes, please pass an arrayref.
93
95 hasp
96 IMPORTANT: until the version 0.06 we have a terrible bug when you try
97 to add the same role in two or more different classes. To avoid this
98 we should not call the "has" method to specify attributes but the
99 method "hasp" (means 'has parameterized').
100
101 method
102 Add one method based on the parameter list, for example.
103
105 See MooX::Role::Parameterized::With package to easily load and apply
106 roles.
107
109 MooseX::Role::Parameterized - Moose version
110
112 Permission is hereby granted, free of charge, to any person
113 obtaining a copy of this software and associated
114 documentation files (the "Software"), to deal in the Software
115 without restriction, including without limitation the rights to
116 use, copy, modify, merge, publish, distribute, sublicense,
117 and/or sell copies of the Software, and to permit persons to
118 whom the Software is furnished to do so, subject to the
119 following conditions:
120
121 The above copyright notice and this permission notice shall
122 be included in all copies or substantial portions of the
123 Software.
124
125 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
126 WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
127 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
128 MERCHANTABILITY, FITNESS FOR A PARTICULAR
129 PURPOSE AND NONINFRINGEMENT. IN NO EVENT
130 SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
131 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
132 LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
133 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
134 CONNECTION WITH THE SOFTWARE OR THE USE OR
135 OTHER DEALINGS IN THE SOFTWARE.
136
138 Tiago Peczenyj <tiago (dot) peczenyj (at) gmail (dot) com>
139
141 Please report any bugs or feature requests on the bugtracker website
142
143
144
145perl v5.34.0 2021-07-22 MooX::Role::Parameterized(3)