1MooseX::Extended::ManuaUls:e:rShCoornttcruitbsu(t3e)d PeMrolosDeoXc:u:mEexntteantdieodn::Manual::Shortcuts(3)
2
3
4
6 MooseX::Extended::Manual::Shortcuts - Shortcuts to make your Moose
7 easier to write
8
10 version 0.35
11
13 When using "field" or "param", we have some attribute shortcuts:
14
15 param name => (
16 isa => NonEmptyStr,
17 writer => 1, # set_name
18 reader => 1, # get_name
19 predicate => 1, # has_name
20 clearer => 1, # clear_name
21 builder => 1, # _build_name
22 );
23
24 sub _build_name ($self) {
25 ...
26 }
27
28 These can also be used when you pass an array reference to the
29 function:
30
31 package Point {
32 use MooseX::Extended types => ['Int'];
33
34 param [ 'x', 'y' ] => (
35 isa => Int,
36 clearer => 1, # clear_x and clear_y available
37 default => 0,
38 );
39 }
40
41 Note that these are shortcuts and they make attributes easier to write
42 and more consistent. However, you can still use full names:
43
44 field authz_delegate => (
45 builder => '_build_my_darned_authz_delegate',
46 );
47
48 These are very similar to MooseX::AttributeShortcuts
49 <https://metacpan.org/pod/MooseX::AttributeShortcuts>, but the naming
50 is slightly different. For example, "clearer => 1" for a "_name"
51 attribute creates a "clear__name" method. but for
52 "MooseX::AttributeShortcuts", it would have been named "_clear_name".
53
54 You still have the "has" function available for defining attributes,
55 but it is unchanged. These shortcuts will not work.
56
57 "writer"
58 If an attribute has "writer" is set to 1 (the number one), a method
59 named "set_$attribute_name" is created.
60
61 This:
62
63 param title => (
64 isa => Undef | NonEmptyStr,
65 default => undef,
66 writer => 1,
67 );
68
69 Is the same as this:
70
71 has title => (
72 is => 'rw', # we change this from 'ro'
73 isa => Undef | NonEmptyStr,
74 default => undef,
75 writer => 'set_title',
76 );
77
78 "reader"
79 By default, the reader (accessor) for the attribute is the same as the
80 name. You can always change this:
81
82 has payload => ( is => 'ro', reader => 'the_payload' );
83
84 However, if you want to change the reader name
85
86 If an attribute has "reader" is set to 1 (the number one), a method
87 named "get_$attribute_name" is created.
88
89 This:
90
91 param title => (
92 isa => Undef | NonEmptyStr,
93 default => undef,
94 reader => 1,
95 );
96
97 Is the same as this:
98
99 has title => (
100 is => 'rw', # we change this from 'ro'
101 isa => Undef | NonEmptyStr,
102 default => undef,
103 reader => 'get_title',
104 );
105
106 "predicate"
107 If an attribute has "predicate" is set to 1 (the number one), a method
108 named "has_$attribute_name" is created.
109
110 This:
111
112 param title => (
113 isa => Undef | NonEmptyStr,
114 default => undef,
115 predicate => 1,
116 );
117
118 Is the same as this:
119
120 has title => (
121 is => 'ro',
122 isa => Undef | NonEmptyStr,
123 default => undef,
124 predicate => 'has_title',
125 );
126
127 "clearer"
128 If an attribute has "clearer" is set to 1 (the number one), a method
129 named "clear_$attribute_name" is created.
130
131 This:
132
133 param title => (
134 isa => Undef | NonEmptyStr,
135 default => undef,
136 clearer => 1,
137 );
138
139 Is the same as this:
140
141 has title => (
142 is => 'ro',
143 isa => Undef | NonEmptyStr,
144 default => undef,
145 clearer => 'clear_title',
146 );
147
148 "builder"
149 If an attribute has "builder" is set to 1 (the number one), a method
150 named "_build_$attribute_name".
151
152 This:
153
154 param title => (
155 isa => NonEmptyStr,
156 builder => 1,
157 );
158
159 Is the same as this:
160
161 has title => (
162 is => 'ro',
163 isa => NonEmptyStr,
164 builder => '_build_title',
165 );
166
167 Obviously, a "private" attribute, such as "_auth_token" would get a
168 build named "_build__auth_token" (note the two underscores between
169 "build" and "auth_token").
170
172 "is => 'rwp'"
173 param name => ( is => 'rwp' );
174
175 The above is equivalent to:
176
177 has name => ( is => 'ro', writer => '_set_name' );
178
179 Of course, it works for "field", too.
180
181 "builder" Code References
182 You may also pass a coderef to `builder`:
183
184 field created => (
185 isa => PositiveInt,
186 lazy => 0,
187 builder => sub {time},
188 );
189
190 This is different from "default => sub {...}" because it will install a
191 "_build_created" method for you. This is useful if you with to allow a
192 subclass to override this method.
193
195 Attributes defined using "param" or "field" which are read-only with no
196 "init_arg" and no default or builder, will result in a warning. If you
197 wish to disable this warning you can.
198
199 no warnings 'MooseX::Extended::naked_fields';
200
201 This warning is only available on Perl >= 5.028.
202
204 Curtis "Ovid" Poe <curtis.poe@gmail.com>
205
207 This software is Copyright (c) 2022 by Curtis "Ovid" Poe.
208
209 This is free software, licensed under:
210
211 The Artistic License 2.0 (GPL Compatible)
212
213
214
215perl v5.38.0 2023-06M-o2o6seX::Extended::Manual::Shortcuts(3)