Class::MOP::Class(3) | User Contributed Perl Documentation | Class::MOP::Class(3) |
Class::MOP::Class - Class Meta Object
version 2.2012
# assuming that class Foo # has been defined, you can # use this for introspection ... # add a method to Foo ... Foo->meta->add_method( 'bar' => sub {...} ) # get a list of all the classes searched # the method dispatcher in the correct order Foo->meta->class_precedence_list() # remove a method from Foo Foo->meta->remove_method('bar'); # or use this to actually create classes ... Class::MOP::Class->create( 'Bar' => ( version => '0.01', superclasses => ['Foo'], attributes => [ Class::MOP::Attribute->new('$bar'), Class::MOP::Attribute->new('$baz'), ], methods => { calculate_bar => sub {...}, construct_baz => sub {...} } ) );
The Class Protocol is the largest and most complex part of the Class::MOP meta-object protocol. It controls the introspection and manipulation of Perl 5 classes, and it can create them as well. The best way to understand what this module can do is to read the documentation for each of its methods.
"Class::MOP::Class" is a subclass of Class::MOP::Module.
These methods all create new "Class::MOP::Class" objects. These objects can represent existing classes or they can be used to create new classes from scratch.
The metaclass object for a given class is a singleton. If you attempt to create a metaclass for the same class twice, you will just get the existing object.
An optional version number for the newly created package.
An optional authority for the newly created package. See "authority" in Class::MOP::Module for more details.
An optional array reference of superclass names.
An optional hash reference of methods for the class. The keys of the hash reference are method names and values are subroutine references.
An optional array reference of Class::MOP::Attribute objects.
Specifies the name to install the "meta" method for this class under. If it is not passed, "meta" is assumed, and if "undef" is explicitly given, no meta method will be installed.
If true, the metaclass that is stored in the global cache will be a weak reference.
Classes created in this way are destroyed once the metaclass they are attached to goes out of scope, and will be removed from Perl's internal symbol table.
All instances of a class with a weakened metaclass keep a special reference to the metaclass object, which prevents the metaclass from going out of scope while any instances exist.
This only works if the instance is based on a hash reference, however.
It accepts the same "superclasses", "methods", and "attributes" parameters that "create" accepts.
It also accepts a "cache" option. If this is "true", then the anonymous class will be cached based on its superclasses and roles. If an existing anonymous class in the cache has the same superclasses and roles, it will be reused.
Anonymous classes default to "weaken => 1" if cache is "false", although this can be overridden.
The purpose of this method is to retrieve a "Class::MOP::Class" object for introspecting an existing class.
If an existing "Class::MOP::Class" object exists for the named package, it will be returned, and any options provided will be ignored!
If the object does not yet exist, it will be created.
The valid options that can be passed to this method are "attribute_metaclass", "method_metaclass", "wrapped_method_metaclass", and "instance_metaclass". These are all optional, and default to the appropriate class in the "Class::MOP" distribution.
These methods are all related to creating and/or cloning object instances.
This is a convenience method for cloning an object instance, then blessing it into the appropriate package.
You could implement a clone method in your class, using this method:
sub clone { my ($self, %params) = @_; $self->meta->clone_object($self, %params); }
You can only rebless an instance into a subclass of its current class. If you pass any additional parameters, these will be treated like constructor parameters and used to initialize the object's attributes. Any existing attributes that are already set will be overwritten.
Before reblessing the instance, this method will call "rebless_instance_away" on the instance's current metaclass. This method will be passed the instance, the new metaclass, and any parameters specified to "rebless_instance". By default, "rebless_instance_away" does nothing; it is merely a hook.
This is a much more dangerous operation than "rebless_instance", especially when multiple inheritance is involved, so use this carefully!
These are a few predicate methods for asking information about the class itself.
This is basically sugar around getting and setting @ISA.
These methods allow you to introspect a class's methods, as well as add, remove, or change methods.
Determining what is truly a method in a Perl 5 class requires some heuristics (aka guessing).
Methods defined outside the package with a fully qualified name ("sub Package::name { ... }") will be included. Similarly, methods named with a fully qualified name using Sub::Name are also included.
However, we attempt to ignore imported functions.
Ultimately, we are using heuristics to determine what truly is a method in a class, and these heuristics may get the wrong answer in some edge cases. However, for most "normal" cases the heuristics work correctly.
The subroutine reference can be a Class::MOP::Method, and you are strongly encouraged to pass a meta method object instead of a code reference. If you do so, that object gets stored as part of the class's method map directly. If not, the meta information will have to be recreated later, and may be incorrect.
If you provide a method object, this method will clone that object if the object's package name does not match the class name. This lets us track the original source of any methods added from other classes (notably Moose roles).
Unlike "get_method", this method will look for the named method in superclasses.
Each method is returned as a hash reference with three keys. The keys are "name", "class", and "code". The "code" key has a Class::MOP::Method object as its value.
The list of methods is distinct.
Because Perl 5 does not have a core concept of attributes in classes, we can only return information about attributes which have been added via this class's methods. We cannot discover information about attributes which are defined in terms of "regular" Perl 5 methods.
NOTE that get_attribute does not search superclasses, for that you need to use "find_attribute_by_name".
Unlike "get_attribute", this attribute will look for the named attribute in superclasses.
The attribute provided will be added to the class.
Any accessor methods defined by the attribute will be added to the class when the attribute is added.
If an attribute of the same name already exists, the old attribute will be removed first.
Removing an attribute also removes any accessor methods defined by the attribute.
However, note that removing an attribute will only affect future object instances created for this class, not existing instances.
These methods provide an API to the core overload functionality.
Making a class immutable "freezes" the class definition. You can no longer call methods which alter the class, such as adding or removing methods or attributes.
Making a class immutable lets us optimize the class by inlining some methods, and also allows us to optimize some methods on the metaclass object itself.
After immutabilization, the metaclass object will cache most informational methods that returns information about methods or attributes. Methods which would alter the class, such as "add_attribute" and "add_method", will throw an error on an immutable metaclass object.
The immutabilization system in Moose takes much greater advantage of the inlining features than Class::MOP itself does.
This method accepts the following options:
These are all booleans indicating whether the specified method(s) should be inlined.
By default, accessors and the constructor are inlined, but not the destructor.
The name of a class which will be used as a parent class for the metaclass object being made immutable. This "trait" implements the post-immutability functionality of the metaclass (but not the transformation itself).
This defaults to Class::MOP::Class::Immutable::Trait.
This is the constructor method name. This defaults to "new".
The name of the method metaclass for constructors. It will be used to generate the inlined constructor. This defaults to "Class::MOP::Method::Constructor".
This is a boolean indicating whether an existing constructor should be replaced when inlining a constructor. This defaults to false.
The name of the method metaclass for destructors. It will be used to generate the inlined destructor. This defaults to "Class::MOP::Method::Denstructor".
This is a boolean indicating whether an existing destructor should be replaced when inlining a destructor. This defaults to false.
Method modifiers are hooks which allow a method to be wrapped with before, after and around method modifiers. Every time a method is called, its modifiers are also called.
A class can modify its own methods, as well as methods defined in parent classes.
How method modifiers work?
Method modifiers work by wrapping the original method and then replacing it in the class's symbol table. The wrappers will handle calling all the modifiers in the appropriate order and preserving the calling context for the original method.
The return values of "before" and "after" modifiers are ignored. This is because their purpose is not to filter the input and output of the primary method (this is done with an around modifier).
This may seem like an odd restriction to some, but doing this allows for simple code to be added at the beginning or end of a method call without altering the function of the wrapped method or placing any extra responsibility on the code of the modifier.
Of course if you have more complex needs, you can use the "around" modifier which allows you to change both the parameters passed to the wrapped method, as well as its return value.
Before and around modifiers are called in last-defined-first-called order, while after modifiers are called in first-defined-first-called order. So the call tree might looks something like this:
before 2 before 1 around 2 around 1 primary around 1 around 2 after 1 after 2
What is the performance impact?
Of course there is a performance cost associated with method modifiers, but we have made every effort to make that cost directly proportional to the number of modifier features you use.
The wrapping method does its best to only do as much work as it absolutely needs to. In order to do this we have moved some of the performance costs to set-up time, where they are easier to amortize.
All this said, our benchmarks have indicated the following:
simple wrapper with no modifiers 100% slower simple wrapper with simple before modifier 400% slower simple wrapper with simple after modifier 450% slower simple wrapper with simple around modifier 500-550% slower simple wrapper with all 3 modifiers 1100% slower
These numbers may seem daunting, but you must remember, every feature comes with some cost. To put things in perspective, just doing a simple "AUTOLOAD" which does nothing but extract the name of the method called and return it costs about 400% over a normal method call.
When the modifier exits, the wrapped method will be called.
The return value of the modifier will be ignored.
When the wrapped methods exits, the modifier will be called.
The return value of the modifier will be ignored.
The first argument passed to the modifier will be a subroutine reference to the wrapped method. The second argument is the object, and after that come any arguments passed when the method is called.
The around modifier can choose to call the original method, as well as what arguments to pass if it does so.
The return value of the modifier is what will be seen by the caller.
It should also be noted that Class::MOP will actually bootstrap this module by installing a number of attribute meta-objects into its metaclass.
This software is copyright (c) 2006 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2019-11-22 | perl v5.34.0 |