Class::MOP::Class - Class Meta Object |
Class::MOP::Class - Class Meta Object
version 2.1605
# 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 the Class::MOP::Module manpage.
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.
Class::MOP::Class
object with the given
package name. It accepts a number of options:
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.
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.
create_anon_class(%options)
>>Class::MOP::Class->create
but it
creates an ``anonymous'' class. In fact, the class does have a name, but
that name is a unique name generated internally by this module.
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.
Class::MOP::Class
object for the
named package. Unlike create
, this method will not create a new
class.
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); }
$instance
to the metaclass's class.
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.
rebless_instance_back($instance)
>>rebless_instance
, except that you can only
rebless an instance into one of its superclasses. Any attributes that
do not exist in the superclass will be deinitialized.
This is a much more dangerous operation than rebless_instance
,
especially when multiple inheritance is involved, so use this carefully!
new_object(%params)
>>__INSTANCE__
key can be passed to
provide an already generated instance, rather than having Class::MOP
generate it for you. This is mostly useful for using Class::MOP with
foreign classes which generate instances using their own constructors.
instance_metaclass
to be used in the
construction of a new instance of the class.
These are a few predicate methods for asking information about the class itself.
<
Class::MOP::Class-
create_anon_class >>.
superclasses(@superclasses)
>>This is basically sugar around getting and setting @ISA
.
class_precedence_list
but with all
duplicates removed.
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 the Sub::Name manpage 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.
get_method($method_name)
>>$method_name
. If the class does not have the specified method, it
returns undef
has_method($method_name)
>>The subroutine reference can be a the Class::MOP::Method manpage, 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).
remove_method($method_name)
>>find_method_by_name($method_name)
>>$method_name
. If the class does not have the specified method, it
returns undef
Unlike get_method
, this method will look for the named method in
superclasses.
find_all_methods_by_name($method_name)
>>Each method is returned as a hash reference with three keys. The keys
are name
, class
, and code
. The code
key has a
the Class::MOP::Method manpage object as its value.
The list of methods is distinct.
find_next_method_by_name($method_name)
>>SUPER::$method_name
would dispatch to.
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.
get_attribute($attribute_name)
>>$attribute_name
. If the class does not have the specified
attribute, it returns undef
.
NOTE that get_attribute does not search superclasses, for that you
need to use find_attribute_by_name
.
has_attribute($attribute_name)
>>find_attribute_by_name($attribute_name)
>>$attribute_name
. If the class does not have the specified
attribute, it returns undef
.
Unlike get_attribute
, this attribute will look for the named
attribute in superclasses.
add_attribute(...)
>>new
method.
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.
remove_attribute($attribute_name)
>>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 the overload manpage functionality.
get_overloaded_operator($op)
>>$op
, if one exists for this class.
has_overloaded_operator($op)
>>$op
is overloaded for this class.
$op
for this class. The $impl
can be a coderef, a
method name, or a the Class::MOP::Overload manpage object. Corresponds to
use overload $op => $impl;
remove_overloaded_operator($op)
>>$op
. Corresponds to no overload $op;
fallback
setting for the package.
set_overload_fallback_value($fallback)
>>fallback
setting for the package.
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.
make_immutable(%options)
>>This method accepts the following options:
method(s)
should be inlined.
By default, accessors and the constructor are inlined, but not the destructor.
This defaults to the Class::MOP::Class::Immutable::Trait manpage.
<
$metaclass-
make_immutable >>. This is useful if you need to temporarily make
a class mutable and then restore immutability as it was before.
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.
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
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 the Class::MOP manpage 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.
Class::MOP::Class - Class Meta Object |