1Object::Pad::MOP::ClassU(s3e)r Contributed Perl DocumentaOtbijoenct::Pad::MOP::Class(3)
2
3
4
6 "Object::Pad::MOP::Class" - meta-object representation of a
7 "Object::Pad" class
8
10 Instances of this class represent a class or role implemented by
11 Object::Pad. Accessors provide information about the class or role, and
12 methods that can alter the class, typically by adding new elements to
13 it, allow a program to extend existing classes.
14
15 Where possible, this API is designed to be compatible with MOP::Class.
16
17 This API should be considered experimental, and will emit warnings to
18 that effect. They can be silenced with
19
20 use Object::Pad qw( :experimental(mop) );
21
22 or
23
24 use Object::Pad::MOP::Class qw( :experimental(mop) );
25
27 for_class
28 $metaclass = Object::Pad::MOP::Class->for_class( $class )
29
30 Since version 0.38.
31
32 Returns the metaclass instance associated with the given class name.
33
34 for_caller
35 $metaclass = Object::Pad::MOP::Class->for_caller;
36
37 Since version 0.38.
38
39 A convenient shortcut for obtaining the metaclass instance of the
40 calling package scope. Often handy during "BEGIN" blocks of the class
41 itself to perform adjustments or additions.
42
43 class Some::Class::Here 1.234 {
44 BEGIN {
45 my $meta = Object::Pad::MOP::Class->for_caller;
46 ...
47 }
48 }
49
50 create_class
51 my $metaclass = Object::Pad::MOP::Class->create_class( $name, %args )
52
53 Since version 0.61.
54
55 Creates a new class of the given name and yields the metaclass for it.
56
57 Takes the following additional named arguments:
58
59 extends => STRING
60 isa => STRING
61 An optional name of a superclass that this class will extend. These
62 options are synonyms; new code should use "isa", as "extends" will
63 eventually be removed.
64
65 Once created, this metaclass must be sealed using the "seal" method
66 before it can be used to actually construct object instances.
67
68 create_role
69 my $metaclass = Object::Pad::MOP::Class->create_role( $name, %args )
70
71 Since version 0.61.
72
73 As "create_class" but creates a role instead of a class.
74
75 begin_class
76 BEGIN {
77 my $metaclass = Object::Pad::MOP::Class->begin_class( $name, %args )
78 ...
79 }
80
81 Since version 0.46.
82
83 A variant of "create_class" which sets the newly-created class as the
84 current complication scope of the surrounding code, allowing it to
85 accept "Object::Pad" syntax forms such as "has" and "method".
86
87 This must be done during "BEGIN" time because of this compiletime
88 effect. It additionally creates a deferred code block at "UNITCHECK"
89 time of its surrounding scope, which is used to finalise the
90 constructed class. In this case you do not need to remember to call
91 "seal" on it; this happens automatically.
92
93 begin_role
94 Since version 0.46.
95
96 As "begin_class" but creates a role instead of a class.
97
99 is_class
100 is_role
101 $bool = $metaclass->is_class
102 $bool = $metaclass->is_role
103
104 Exactly one of these methods will return true, depending on whether
105 this metaclass instance represents a true "class", or a "role".
106
107 name
108 $name = $metaclass->name
109
110 Returns the name of the class, as a plain string.
111
112 superclasses
113 @classes = $metaclass->superclasses
114
115 Returns a list of superclasses, as Object::Pad::MOP::Class instances.
116
117 Because "Object::Pad" does not support multiple superclasses, this list
118 will contain at most one item.
119
120 direct_roles
121 @roles = $metaclass->direct_roles
122
123 Returns a list of the roles introduced by this class (i.e. added by
124 `does` declarations but not inherited from the superclass), as
125 Object::Pad::MOP::Class instances.
126
127 This method is also aliased as "roles".
128
129 all_roles
130 @roles = $metaclass->all_roles
131
132 Since version 0.56.
133
134 Returns a list of all the roles implemented by this class (i.e.
135 including those inherited from the superclass), as
136 Object::Pad::MOP::Class instances.
137
138 add_role
139 $metaclass->add_role( $rolename )
140 $metaclass->add_role( $rolemeta )
141
142 Since version 0.56.
143
144 Adds a new role to the list of those implemented by the class.
145
146 The new role can be specified either as a plain string giving its name,
147 or as an "Object::Pad::MOP::Class" meta instance directly.
148
149 Before version 0.56 this was called "compose_role".
150
151 add_BUILD
152 $metaclass->add_BUILD( $code )
153
154 Adds a new "BUILD" block to the class, as a CODE reference.
155
156 add_method
157 $metamethod = $metaclass->add_method( $name, %args, $code )
158
159 Adds a new named method to the class under the given name, as CODE
160 reference.
161
162 Returns an instance of Object::Pad::MOP::Method to represent it.
163
164 Recognises the following additional named arguments:
165
166 common => BOOL
167 Since version 0.62.
168
169 If true, the method is a class-common method.
170
171 get_direct_method
172 $metamethod = $metaclass->get_direct_method( $name )
173
174 Returns an instance of Object::Pad::MOP::Method to represent the method
175 of the given name, if one exists. If not an exception is thrown.
176
177 This can only see directly-applied methods; that is, methods created by
178 the "method" keyword on the class itself, or added via "add_method".
179 This will not see other names in the package stash, even if they
180 contain a "CODE" slot, nor will it see methods inherited from a
181 superclass.
182
183 This is also aliased as "get_own_method" for compatibility with the
184 MOP::Class interface.
185
186 get_method
187 $metamethod = $metaclass->get_method( $name )
188
189 Since version 0.57.
190
191 Returns an instance of Object::Pad::MOP::Method to represent the method
192 of the given name, if one exists. If not an exception is thrown.
193
194 This will additionally search superclasses, and may return a method
195 belonging to a parent class.
196
197 direct_methods
198 @metamethods = $metaclass->direct_methods
199
200 Since version 0.57.
201
202 Returns a list of Object::Pad::MOP::Method instances to represent all
203 the direct methods of the class. This list may be empty.
204
205 all_methods
206 @metamethods = $metaclass->all_methods
207
208 Since version 0.57.
209
210 Returns a list of Object::Pad::MOP::Method instances to represent all
211 the methods of the class, including those inherited from superclasses.
212 This list may be empty.
213
214 add_field
215 $metafield = $metaclass->add_field( $name, %args )
216
217 since version 0.60.
218
219 Adds a new field to the class, using the given name (which must begin
220 with the sigil character "$", "@" or "%").
221
222 Recognises the following additional named arguments:
223
224 default => SCALAR
225 Since version 0.43.
226
227 Provides a default value for the field; similar to using the syntax
228
229 has $field = SCALAR;
230
231 This value may be "undef", to set the value as being optional if it
232 additionally has a parameter name.
233
234 param => STRING
235 Since version 0.43.
236
237 Provides a parameter name for the field; similar to setting it
238 using the ":param" attribute. This parameter will be required
239 unless a default value is set (such value may still be "undef").
240
241 reader => STRING
242 writer => STRING
243 mutator => STRING
244 Since version 0.46.
245
246 accessor => STRING
247 Since version 0.56.
248
249 Provides method names for generated reader, writer, lvalue-mutator
250 or reader+writer accessor methods, similar to setting them via the
251 ":reader", ":writer", ":mutator" or ":accessor" attributes.
252
253 weak => BOOL
254 Since version 0.46.
255
256 If true, reference values assigned into the field by the
257 constructor or accessor methods will be weakened, similar to
258 setting the ":weak" attribute.
259
260 Returns an instance of Object::Pad::MOP::Field to represent it.
261
262 add_slot
263 $metafield = $metaclass->add_slot( $name, %args )
264
265 Now deprecated.
266
267 Back-compatibility alias for "add_field".
268
269 get_field
270 $metafield = $metaclass->get_field( $name )
271
272 Since version 0.60.
273
274 Returns an instance of Object::Pad::MOP::Field to represent the field
275 of the given name, if one exists. If not an exception is thrown.
276
277 get_slot
278 $metafield = $metaclass->get_slot( $name )
279
280 Now deprecated.
281
282 Back-compatibility alias for "get_field".
283
284 fields
285 @metafields = $metaclass->fields
286
287 Since version 0.60.
288
289 Returns a list of Object::Pad::MOP::Field instances to represent all
290 the fields of the class. This list may be empty.
291
292 slots
293 @metafields = $metaclass->slots
294
295 Since version 0.42; now deprecated.
296
297 Back-compatibility alias for "fields".
298
299 add_required_method
300 $metaclass->add_required_method( $name )
301
302 Since version 0.61.
303
304 Adds a new required method to the role, whose name is given as a plain
305 string.
306
307 Currently returns nothing. This should be considered temporary, as
308 eventually a metatype for required methods will be added, at which
309 point this method can return instances of it. It may also take
310 additional parameters to define the required method with. Currently
311 extra parameters are not permitted.
312
313 required_method_names
314 @names = $metaclass->required_method_names
315
316 Since version 0.61.
317
318 Returns a list names of required methods for the role, as plain
319 strings.
320
321 This should be considered a temporary method. Currently there is no
322 metatype for required methods, so they are represented as plain
323 strings. Eventually a type may be defined and a "required_methods"
324 method will be added.
325
326 seal
327 $metaclass->seal
328
329 Since version 0.61.
330
331 If the metaclass was created by "create_class" or "create_role", this
332 method must be called once everything has been added into it, as the
333 class will not yet be ready to construct actual object instances before
334 this is done.
335
337 Paul Evans <leonerd@leonerd.org.uk>
338
339
340
341perl v5.36.1 2023-05-15 Object::Pad::MOP::Class(3)