perldoc > Class::MOP::Class

📛 NAME

Class::MOP::Class - Class Meta Object

🚀 Quick Reference

Use CaseCommandDescription
🔍 Introspect existing classClass::MOP::Class->initialize('Foo')Get a metaclass object for an existing class
➕ Add method$metaclass->add_method('bar', sub {...})Add a method to the class
➖ Remove method$metaclass->remove_method('bar')Remove a method from the class
📋 Get class precedence list$metaclass->class_precedence_list()List ancestors in method dispatch order
đŸ—ī¸ Create a new classClass::MOP::Class->create('Bar', superclasses => ['Foo'], ...)Create a new class from scratch
🆕 Create anonymous classClass::MOP::Class->create_anon_class(...)Create a temporary anonymous class
🧊 Make class immutable$metaclass->make_immutable()Freeze class definition for optimization
🔄 Clone object$metaclass->clone_object($instance, %params)Clone an instance with optional overrides

đŸ“Ļ VERSION

version 2.2200

📋 SYNOPSIS

# 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 {...}
        }
    )
);

📝 DESCRIPTION

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.

đŸ§Ŧ INHERITANCE

"Class::MOP::Class" is a subclass of Class::MOP::Module.

âš™ī¸ METHODS

đŸ—ī¸ Class construction

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->create($package_name, %options)

This method creates a new "Class::MOP::Class" object with the given package name. It accepts a number of options:

Class::MOP::Class->create_anon_class(%options)

This method works just like "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->initialize($package_name, %options)

This method will initialize a "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.

đŸ§Ŧ Object instance construction and cloning

These methods are all related to creating and/or cloning object instances.

$metaclass->clone_object($instance, %params)

This method clones an existing object instance. Any parameters you provide are will override existing attribute values in the object. 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);
}

$metaclass->rebless_instance($instance, %params)

This method changes the class of $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.

$metaclass->rebless_instance_back($instance)

Does the same thing as "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!

$metaclass->new_object(%params)

This method is used to create a new object of the metaclass's class. Any parameters you provide are used to initialize the instance's attributes. A special "__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.

$metaclass->instance_metaclass

Returns the class name of the instance metaclass. See Class::MOP::Instance for more information on the instance metaclass.

$metaclass->get_meta_instance

Returns an instance of the "instance_metaclass" to be used in the construction of a new instance of the class.

â„šī¸ Informational predicates

These are a few predicate methods for asking information about the class itself.

🔗 Inheritance Relationships

🔧 Method introspection and creation

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 or Sub::Util 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.

đŸ“Ļ Attribute introspection and creation

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.

⚡ Overload introspection and creation

These methods provide an API to the core overload functionality.

🧊 Class Immutability

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.

$metaclass->make_immutable(%options)

This method will create an immutable transformer and use it to make the class and its metaclass object immutable, and returns true (you should not rely on the details of this value apart from its truth). This method accepts the following options:

$metaclass->immutable_options

Returns a hash of the options used when making the class immutable, including both defaults and anything supplied by the user in the call to "$metaclass->make_immutable". This is useful if you need to temporarily make a class mutable and then restore immutability as it was before.

$metaclass->make_mutable

Calling this method reverse the immutabilization transformation.

🔧 Method Modifiers

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:

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.

🔍 Introspection

Class::MOP::Class->meta

This will return a Class::MOP::Class instance for this class. It should also be noted that Class::MOP will actually bootstrap this module by installing a number of attribute meta-objects into its metaclass.

đŸ‘Ĩ AUTHORS

📄 COPYRIGHT AND LICENSE

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
📛 NAME 🚀 Quick Reference đŸ“Ļ VERSION 📋 SYNOPSIS 📝 DESCRIPTION đŸ§Ŧ INHERITANCE âš™ī¸ METHODS
đŸ—ī¸ Class construction đŸ§Ŧ Object instance construction and cloning â„šī¸ Informational predicates 🔗 Inheritance Relationships 🔧 Method introspection and creation đŸ“Ļ Attribute introspection and creation ⚡ Overload introspection and creation 🧊 Class Immutability 🔧 Method Modifiers 🔍 Introspection
đŸ‘Ĩ AUTHORS 📄 COPYRIGHT AND LICENSE

Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-12 04:34 @2600:1f28:365:80b0:194e:56dd:d3a6:4135
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^