1Class::MOP::Class(3) User Contributed Perl Documentation Class::MOP::Class(3)
2
3
4
6 Class::MOP::Class - Class Meta Object
7
9 # assuming that class Foo
10 # has been defined, you can
11
12 # use this for introspection ...
13
14 # add a method to Foo ...
15 Foo->meta->add_method( 'bar' => sub {...} )
16
17 # get a list of all the classes searched
18 # the method dispatcher in the correct order
19 Foo->meta->class_precedence_list()
20
21 # remove a method from Foo
22 Foo->meta->remove_method('bar');
23
24 # or use this to actually create classes ...
25
26 Class::MOP::Class->create(
27 'Bar' => (
28 version => '0.01',
29 superclasses => ['Foo'],
30 attributes => [
31 Class::MOP::Attribute->new('$bar'),
32 Class::MOP::Attribute->new('$baz'),
33 ],
34 methods => {
35 calculate_bar => sub {...},
36 construct_baz => sub {...}
37 }
38 )
39 );
40
42 The Class Protocol is the largest and most complex part of the
43 Class::MOP meta-object protocol. It controls the introspection and
44 manipulation of Perl 5 classes, and it can create them as well. The
45 best way to understand what this module can do is to read the
46 documentation for each of its methods.
47
49 "Class::MOP::Class" is a subclass of Class::MOP::Module.
50
52 Class construction
53 These methods all create new "Class::MOP::Class" objects. These objects
54 can represent existing classes or they can be used to create new
55 classes from scratch.
56
57 The metaclass object for a given class is a singleton. If you attempt
58 to create a metaclass for the same class twice, you will just get the
59 existing object.
60
61 Class::MOP::Class->create($package_name, %options)
62 This method creates a new "Class::MOP::Class" object with the given
63 package name. It accepts a number of options:
64
65 · version
66
67 An optional version number for the newly created package.
68
69 · authority
70
71 An optional authority for the newly created package.
72
73 · superclasses
74
75 An optional array reference of superclass names.
76
77 · methods
78
79 An optional hash reference of methods for the class. The
80 keys of the hash reference are method names and values are
81 subroutine references.
82
83 · attributes
84
85 An optional array reference of Class::MOP::Attribute
86 objects.
87
88 Class::MOP::Class->create_anon_class(%options)
89 This method works just like "Class::MOP::Class->create" but it
90 creates an "anonymous" class. In fact, the class does have a name,
91 but that name is a unique name generated internally by this module.
92
93 It accepts the same "superclasses", "methods", and "attributes"
94 parameters that "create" accepts.
95
96 Anonymous classes are destroyed once the metaclass they are
97 attached to goes out of scope, and will be removed from Perl's
98 internal symbol table.
99
100 All instances of an anonymous class keep a special reference to the
101 metaclass object, which prevents the metaclass from going out of
102 scope while any instances exist.
103
104 This only works if the instance is based on a hash reference,
105 however.
106
107 Class::MOP::Class->initialize($package_name, %options)
108 This method will initialize a "Class::MOP::Class" object for the
109 named package. Unlike "create", this method will not create a new
110 class.
111
112 The purpose of this method is to retrieve a "Class::MOP::Class"
113 object for introspecting an existing class.
114
115 If an existing "Class::MOP::Class" object exists for the named
116 package, it will be returned, and any options provided will be
117 ignored!
118
119 If the object does not yet exist, it will be created.
120
121 The valid options that can be passed to this method are
122 "attribute_metaclass", "method_metaclass",
123 "wrapped_method_metaclass", and "instance_metaclass". These are all
124 optional, and default to the appropriate class in the "Class::MOP"
125 distribution.
126
127 Object instance construction and cloning
128 These methods are all related to creating and/or cloning object
129 instances.
130
131 $metaclass->clone_object($instance, %params)
132 This method clones an existing object instance. Any parameters you
133 provide are will override existing attribute values in the object.
134
135 This is a convenience method for cloning an object instance, then
136 blessing it into the appropriate package.
137
138 You could implement a clone method in your class, using this
139 method:
140
141 sub clone {
142 my ($self, %params) = @_;
143 $self->meta->clone_object($self, %params);
144 }
145
146 $metaclass->rebless_instance($instance, %params)
147 This method changes the class of $instance to the metaclass's
148 class.
149
150 You can only rebless an instance into a subclass of its current
151 class. If you pass any additional parameters, these will be treated
152 like constructor parameters and used to initialize the object's
153 attributes. Any existing attributes that are already set will be
154 overwritten.
155
156 Before reblessing the instance, this method will call
157 "rebless_instance_away" on the instance's current metaclass. This
158 method will be passed the instance, the new metaclass, and any
159 parameters specified to "rebless_instance". By default,
160 "rebless_instance_away" does nothing; it is merely a hook.
161
162 $metaclass->rebless_instance_back($instance)
163 Does the same thing as "rebless_instance", except that you can only
164 rebless an instance into one of its superclasses. Any attributes
165 that do not exist in the superclass will be deinitialized.
166
167 This is a much more dangerous operation than "rebless_instance",
168 especially when multiple inheritance is involved, so use this
169 carefully!
170
171 $metaclass->new_object(%params)
172 This method is used to create a new object of the metaclass's
173 class. Any parameters you provide are used to initialize the
174 instance's attributes. A special "__INSTANCE__" key can be passed
175 to provide an already generated instance, rather than having
176 Class::MOP generate it for you. This is mostly useful for using
177 Class::MOP with foreign classes which generate instances using
178 their own constructors.
179
180 $metaclass->instance_metaclass
181 Returns the class name of the instance metaclass. See
182 Class::MOP::Instance for more information on the instance
183 metaclass.
184
185 $metaclass->get_meta_instance
186 Returns an instance of the "instance_metaclass" to be used in the
187 construction of a new instance of the class.
188
189 Informational predicates
190 These are a few predicate methods for asking information about the
191 class itself.
192
193 $metaclass->is_anon_class
194 This returns true if the class was created by calling
195 "Class::MOP::Class->create_anon_class".
196
197 $metaclass->is_mutable
198 This returns true if the class is still mutable.
199
200 $metaclass->is_immutable
201 This returns true if the class has been made immutable.
202
203 $metaclass->is_pristine
204 A class is not pristine if it has non-inherited attributes or if it
205 has any generated methods.
206
207 Inheritance Relationships
208 $metaclass->superclasses(@superclasses)
209 This is a read-write accessor which represents the superclass
210 relationships of the metaclass's class.
211
212 This is basically sugar around getting and setting @ISA.
213
214 $metaclass->class_precedence_list
215 This returns a list of all of the class's ancestor classes. The
216 classes are returned in method dispatch order.
217
218 $metaclass->linearized_isa
219 This returns a list based on "class_precedence_list" but with all
220 duplicates removed.
221
222 $metaclass->subclasses
223 This returns a list of all subclasses for this class, even indirect
224 subclasses.
225
226 $metaclass->direct_subclasses
227 This returns a list of immediate subclasses for this class, which
228 does not include indirect subclasses.
229
230 Method introspection and creation
231 These methods allow you to introspect a class's methods, as well as
232 add, remove, or change methods.
233
234 Determining what is truly a method in a Perl 5 class requires some
235 heuristics (aka guessing).
236
237 Methods defined outside the package with a fully qualified name ("sub
238 Package::name { ... }") will be included. Similarly, methods named with
239 a fully qualified name using Sub::Name are also included.
240
241 However, we attempt to ignore imported functions.
242
243 Ultimately, we are using heuristics to determine what truly is a method
244 in a class, and these heuristics may get the wrong answer in some edge
245 cases. However, for most "normal" cases the heuristics work correctly.
246
247 $metaclass->get_method($method_name)
248 This will return a Class::MOP::Method for the specified
249 $method_name. If the class does not have the specified method, it
250 returns "undef"
251
252 $metaclass->has_method($method_name)
253 Returns a boolean indicating whether or not the class defines the
254 named method. It does not include methods inherited from parent
255 classes.
256
257 $metaclass->get_method_list
258 This will return a list of method names for all methods defined in
259 this class.
260
261 $metaclass->add_method($method_name, $method)
262 This method takes a method name and a subroutine reference, and
263 adds the method to the class.
264
265 The subroutine reference can be a Class::MOP::Method, and you are
266 strongly encouraged to pass a meta method object instead of a code
267 reference. If you do so, that object gets stored as part of the
268 class's method map directly. If not, the meta information will have
269 to be recreated later, and may be incorrect.
270
271 If you provide a method object, this method will clone that object
272 if the object's package name does not match the class name. This
273 lets us track the original source of any methods added from other
274 classes (notably Moose roles).
275
276 $metaclass->remove_method($method_name)
277 Remove the named method from the class. This method returns the
278 Class::MOP::Method object for the method.
279
280 $metaclass->method_metaclass
281 Returns the class name of the method metaclass, see
282 Class::MOP::Method for more information on the method metaclass.
283
284 $metaclass->wrapped_method_metaclass
285 Returns the class name of the wrapped method metaclass, see
286 Class::MOP::Method::Wrapped for more information on the wrapped
287 method metaclass.
288
289 $metaclass->get_all_methods
290 This will traverse the inheritance hierarchy and return a list of
291 all the Class::MOP::Method objects for this class and its parents.
292
293 $metaclass->find_method_by_name($method_name)
294 This will return a Class::MOP::Method for the specified
295 $method_name. If the class does not have the specified method, it
296 returns "undef"
297
298 Unlike "get_method", this method will look for the named method in
299 superclasses.
300
301 $metaclass->get_all_method_names
302 This will return a list of method names for all of this class's
303 methods, including inherited methods.
304
305 $metaclass->find_all_methods_by_name($method_name)
306 This method looks for the named method in the class and all of its
307 parents. It returns every matching method it finds in the
308 inheritance tree, so it returns a list of methods.
309
310 Each method is returned as a hash reference with three keys. The
311 keys are "name", "class", and "code". The "code" key has a
312 Class::MOP::Method object as its value.
313
314 The list of methods is distinct.
315
316 $metaclass->find_next_method_by_name($method_name)
317 This method returns the first method in any superclass matching the
318 given name. It is effectively the method that "SUPER::$method_name"
319 would dispatch to.
320
321 Attribute introspection and creation
322 Because Perl 5 does not have a core concept of attributes in classes,
323 we can only return information about attributes which have been added
324 via this class's methods. We cannot discover information about
325 attributes which are defined in terms of "regular" Perl 5 methods.
326
327 $metaclass->get_attribute($attribute_name)
328 This will return a Class::MOP::Attribute for the specified
329 $attribute_name. If the class does not have the specified
330 attribute, it returns "undef".
331
332 NOTE that get_attribute does not search superclasses, for that you
333 need to use "find_attribute_by_name".
334
335 $metaclass->has_attribute($attribute_name)
336 Returns a boolean indicating whether or not the class defines the
337 named attribute. It does not include attributes inherited from
338 parent classes.
339
340 $metaclass->get_attribute_list
341 This will return a list of attributes names for all attributes
342 defined in this class. Note that this operates on the current
343 class only, it does not traverse the inheritance hierarchy.
344
345 $metaclass->get_all_attributes
346 This will traverse the inheritance hierarchy and return a list of
347 all the Class::MOP::Attribute objects for this class and its
348 parents.
349
350 $metaclass->find_attribute_by_name($attribute_name)
351 This will return a Class::MOP::Attribute for the specified
352 $attribute_name. If the class does not have the specified
353 attribute, it returns "undef".
354
355 Unlike "get_attribute", this attribute will look for the named
356 attribute in superclasses.
357
358 $metaclass->add_attribute(...)
359 This method accepts either an existing Class::MOP::Attribute object
360 or parameters suitable for passing to that class's "new" method.
361
362 The attribute provided will be added to the class.
363
364 Any accessor methods defined by the attribute will be added to the
365 class when the attribute is added.
366
367 If an attribute of the same name already exists, the old attribute
368 will be removed first.
369
370 $metaclass->remove_attribute($attribute_name)
371 This will remove the named attribute from the class, and
372 Class::MOP::Attribute object.
373
374 Removing an attribute also removes any accessor methods defined by
375 the attribute.
376
377 However, note that removing an attribute will only affect future
378 object instances created for this class, not existing instances.
379
380 $metaclass->attribute_metaclass
381 Returns the class name of the attribute metaclass for this class.
382 By default, this is Class::MOP::Attribute.
383
384 Class Immutability
385 Making a class immutable "freezes" the class definition. You can no
386 longer call methods which alter the class, such as adding or removing
387 methods or attributes.
388
389 Making a class immutable lets us optimize the class by inlining some
390 methods, and also allows us to optimize some methods on the metaclass
391 object itself.
392
393 After immutabilization, the metaclass object will cache most
394 informational methods that returns information about methods or
395 attributes. Methods which would alter the class, such as
396 "add_attribute" and "add_method", will throw an error on an immutable
397 metaclass object.
398
399 The immutabilization system in Moose takes much greater advantage of
400 the inlining features than Class::MOP itself does.
401
402 $metaclass->make_immutable(%options)
403 This method will create an immutable transformer and use it to make
404 the class and its metaclass object immutable.
405
406 This method accepts the following options:
407
408 · inline_accessors
409
410 · inline_constructor
411
412 · inline_destructor
413
414 These are all booleans indicating whether the specified
415 method(s) should be inlined.
416
417 By default, accessors and the constructor are inlined, but
418 not the destructor.
419
420 · immutable_trait
421
422 The name of a class which will be used as a parent class
423 for the metaclass object being made immutable. This "trait"
424 implements the post-immutability functionality of the
425 metaclass (but not the transformation itself).
426
427 This defaults to Class::MOP::Class::Immutable::Trait.
428
429 · constructor_name
430
431 This is the constructor method name. This defaults to
432 "new".
433
434 · constructor_class
435
436 The name of the method metaclass for constructors. It will
437 be used to generate the inlined constructor. This defaults
438 to "Class::MOP::Method::Constructor".
439
440 · replace_constructor
441
442 This is a boolean indicating whether an existing
443 constructor should be replaced when inlining a constructor.
444 This defaults to false.
445
446 · destructor_class
447
448 The name of the method metaclass for destructors. It will
449 be used to generate the inlined destructor. This defaults
450 to "Class::MOP::Method::Denstructor".
451
452 · replace_destructor
453
454 This is a boolean indicating whether an existing destructor
455 should be replaced when inlining a destructor. This
456 defaults to false.
457
458 $metaclass->immutable_options
459 Returns a hash of the options used when making the class immutable,
460 including both defaults and anything supplied by the user in the
461 call to "$metaclass->make_immutable". This is useful if you need to
462 temporarily make a class mutable and then restore immutability as
463 it was before.
464
465 $metaclass->make_mutable
466 Calling this method reverse the immutabilization transformation.
467
468 Method Modifiers
469 Method modifiers are hooks which allow a method to be wrapped with
470 before, after and around method modifiers. Every time a method is
471 called, its modifiers are also called.
472
473 A class can modify its own methods, as well as methods defined in
474 parent classes.
475
476 How method modifiers work?
477
478 Method modifiers work by wrapping the original method and then
479 replacing it in the class's symbol table. The wrappers will handle
480 calling all the modifiers in the appropriate order and preserving the
481 calling context for the original method.
482
483 The return values of "before" and "after" modifiers are ignored. This
484 is because their purpose is not to filter the input and output of the
485 primary method (this is done with an around modifier).
486
487 This may seem like an odd restriction to some, but doing this allows
488 for simple code to be added at the beginning or end of a method call
489 without altering the function of the wrapped method or placing any
490 extra responsibility on the code of the modifier.
491
492 Of course if you have more complex needs, you can use the "around"
493 modifier which allows you to change both the parameters passed to the
494 wrapped method, as well as its return value.
495
496 Before and around modifiers are called in last-defined-first-called
497 order, while after modifiers are called in first-defined-first-called
498 order. So the call tree might looks something like this:
499
500 before 2
501 before 1
502 around 2
503 around 1
504 primary
505 around 1
506 around 2
507 after 1
508 after 2
509
510 What is the performance impact?
511
512 Of course there is a performance cost associated with method modifiers,
513 but we have made every effort to make that cost directly proportional
514 to the number of modifier features you use.
515
516 The wrapping method does its best to only do as much work as it
517 absolutely needs to. In order to do this we have moved some of the
518 performance costs to set-up time, where they are easier to amortize.
519
520 All this said, our benchmarks have indicated the following:
521
522 simple wrapper with no modifiers 100% slower
523 simple wrapper with simple before modifier 400% slower
524 simple wrapper with simple after modifier 450% slower
525 simple wrapper with simple around modifier 500-550% slower
526 simple wrapper with all 3 modifiers 1100% slower
527
528 These numbers may seem daunting, but you must remember, every feature
529 comes with some cost. To put things in perspective, just doing a simple
530 "AUTOLOAD" which does nothing but extract the name of the method called
531 and return it costs about 400% over a normal method call.
532
533 $metaclass->add_before_method_modifier($method_name, $code)
534 This wraps the specified method with the supplied subroutine
535 reference. The modifier will be called as a method itself, and will
536 receive the same arguments as are passed to the method.
537
538 When the modifier exits, the wrapped method will be called.
539
540 The return value of the modifier will be ignored.
541
542 $metaclass->add_after_method_modifier($method_name, $code)
543 This wraps the specified method with the supplied subroutine
544 reference. The modifier will be called as a method itself, and will
545 receive the same arguments as are passed to the method.
546
547 When the wrapped methods exits, the modifier will be called.
548
549 The return value of the modifier will be ignored.
550
551 $metaclass->add_around_method_modifier($method_name, $code)
552 This wraps the specified method with the supplied subroutine
553 reference.
554
555 The first argument passed to the modifier will be a subroutine
556 reference to the wrapped method. The second argument is the object,
557 and after that come any arguments passed when the method is called.
558
559 The around modifier can choose to call the original method, as well
560 as what arguments to pass if it does so.
561
562 The return value of the modifier is what will be seen by the
563 caller.
564
565 Introspection
566 Class::MOP::Class->meta
567 This will return a Class::MOP::Class instance for this class.
568
569 It should also be noted that Class::MOP will actually bootstrap
570 this module by installing a number of attribute meta-objects into
571 its metaclass.
572
574 Stevan Little <stevan@iinteractive.com>
575
577 Copyright 2006-2010 by Infinity Interactive, Inc.
578
579 <http://www.iinteractive.com>
580
581 This library is free software; you can redistribute it and/or modify it
582 under the same terms as Perl itself.
583
584
585
586perl v5.12.2 2010-09-13 Class::MOP::Class(3)