.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Class::MOP 3"
.TH Class::MOP 3 "2021-11-07" "perl v5.26.3" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Class::MOP \- A Meta Object Protocol for Perl 5
.SH "VERSION"
.IX Header "VERSION"
version 2.2201
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This module is a fully functioning meta object protocol for the
Perl 5 object system. It makes no attempt to change the behavior or
characteristics of the Perl 5 object system, only to create a
protocol for its manipulation and introspection.
.PP
That said, it does attempt to create the tools for building a rich set
of extensions to the Perl 5 object system. Every attempt has been made
to abide by the spirit of the Perl 5 object system that we all know
and love.
.PP
This documentation is sparse on conceptual details. We suggest looking
at the items listed in the \*(L"\s-1SEE ALSO\*(R"\s0 section for more
information. In particular the book \*(L"The Art of the Meta Object
Protocol\*(R" was very influential in the development of this system.
.SS "What is a Meta Object Protocol?"
.IX Subsection "What is a Meta Object Protocol?"
A meta object protocol is an \s-1API\s0 to an object system.
.PP
To be more specific, it abstracts the components of an object system
(classes, object, methods, object attributes, etc.). These
abstractions can then be used to inspect and manipulate the object
system which they describe.
.PP
It can be said that there are two MOPs for any object system; the
implicit \s-1MOP\s0 and the explicit \s-1MOP.\s0 The implicit \s-1MOP\s0 handles things
like method dispatch or inheritance, which happen automatically as
part of how the object system works. The explicit \s-1MOP\s0 typically
handles the introspection/reflection features of the object system.
.PP
All object systems have implicit MOPs. Without one, they would not
work. Explicit MOPs are much less common, and depending on the
language can vary from restrictive (Reflection in Java or C#) to wide
open (\s-1CLOS\s0 is a perfect example).
.SS "Yet Another Class Builder! Why?"
.IX Subsection "Yet Another Class Builder! Why?"
This is \fBnot\fR a class builder so much as a \fIclass builder
\&\f(BIbuilder\fI\fR. The intent is that an end user will not use this module
directly, but instead this module is used by module authors to build
extensions and features onto the Perl 5 object system.
.PP
This system is used by Moose, which supplies a powerful class
builder system built entirely on top of \f(CW\*(C`Class::MOP\*(C'\fR.
.SS "Who is this module for?"
.IX Subsection "Who is this module for?"
This module is for anyone who has ever created or wanted to create a
module for the Class:: namespace. The tools which this module provides
make doing complex Perl 5 wizardry simpler, by removing such barriers
as the need to hack symbol tables, or understand the fine details of
method dispatch.
.SS "What changes do I have to make to use this module?"
.IX Subsection "What changes do I have to make to use this module?"
This module was designed to be as unobtrusive as possible. Many of its
features are accessible without \fBany\fR change to your existing
code. It is meant to be a complement to your existing code and not an
intrusion on your code base. Unlike many other \fBClass::\fR modules,
this module \fBdoes not\fR require you subclass it, or even that you
\&\f(CW\*(C`use\*(C'\fR it in within your module's package.
.PP
The only features which require additions to your code are the
attribute handling and instance construction features, and these are
both completely optional features. The only reason for this is because
Perl 5's object system does not actually have these features built
in. More information about this feature can be found below.
.SS "About Performance"
.IX Subsection "About Performance"
It is a common misconception that explicit MOPs are a performance hit.
This is not a universal truth, it is a side-effect of some specific
implementations. For instance, using Java reflection is slow because
the \s-1JVM\s0 cannot take advantage of any compiler optimizations, and the
\&\s-1JVM\s0 has to deal with much more runtime type information as well.
.PP
Reflection in C# is marginally better as it was designed into the
language and runtime (the \s-1CLR\s0). In contrast, \s-1CLOS\s0 (the Common Lisp
Object System) was built to support an explicit \s-1MOP,\s0 and so
performance is tuned for it.
.PP
This library in particular does its absolute best to avoid putting
\&\fBany\fR drain at all upon your code's performance. In fact, by itself
it does nothing to affect your existing code. So you only pay for what
you actually use.
.SS "About Metaclass compatibility"
.IX Subsection "About Metaclass compatibility"
This module makes sure that all metaclasses created are both upwards
and downwards compatible. The topic of metaclass compatibility is
highly esoteric and is something only encountered when doing deep and
involved metaclass hacking. There are two basic kinds of metaclass
incompatibility; upwards and downwards.
.PP
Upwards metaclass compatibility means that the metaclass of a
given class is either the same as (or a subclass of) all of the
metaclasses of the class's ancestors.
.PP
Downward metaclass compatibility means that the metaclasses of a
given class's ancestors are all the same as (or a subclass of) that
class's metaclass.
.PP
Here is a diagram showing a set of two classes (\f(CW\*(C`A\*(C'\fR and \f(CW\*(C`B\*(C'\fR) and
two metaclasses (\f(CW\*(C`Meta::A\*(C'\fR and \f(CW\*(C`Meta::B\*(C'\fR) which have correct
metaclass compatibility both upwards and downwards.
.PP
.Vb 8
\& +\-\-\-\-\-\-\-\-\-+ +\-\-\-\-\-\-\-\-\-+
\& | Meta::A |<\-\-\-\-| Meta::B | <....... (instance of )
\& +\-\-\-\-\-\-\-\-\-+ +\-\-\-\-\-\-\-\-\-+ <\-\-\-\-\-\-\- (inherits from)
\& ^ ^
\& : :
\& +\-\-\-\-\-\-\-\-\-+ +\-\-\-\-\-\-\-\-\-+
\& | A |<\-\-\-\-| B |
\& +\-\-\-\-\-\-\-\-\-+ +\-\-\-\-\-\-\-\-\-+
.Ve
.PP
In actuality, \fIall\fR of a class's metaclasses must be compatible,
not just the class metaclass. That includes the instance, attribute,
and method metaclasses, as well as the constructor and destructor
classes.
.PP
\&\f(CW\*(C`Class::MOP\*(C'\fR will attempt to fix some simple types of
incompatibilities. If all the metaclasses for the parent class are
\&\fIsubclasses\fR of the child's metaclasses then we can simply replace
the child's metaclasses with the parent's. In addition, if the child
is missing a metaclass that the parent has, we can also just make the
child use the parent's metaclass.
.PP
As I said this is a highly esoteric topic and one you will only run
into if you do a lot of subclassing of Class::MOP::Class. If you
are interested in why this is an issue see the paper \fIUniform and
safe metaclass composition\fR linked to in the \*(L"\s-1SEE ALSO\*(R"\s0 section of
this document.
.SS "Using custom metaclasses"
.IX Subsection "Using custom metaclasses"
Always use the metaclass pragma when using a custom metaclass, this
will ensure the proper initialization order and not accidentally
create an incorrect type of metaclass for you. This is a very rare
problem, and one which can only occur if you are doing deep metaclass
programming. So in other words, don't worry about it.
.PP
Note that if you're using Moose we encourage you to \fInot\fR use the
metaclass pragma, and instead use Moose::Util::MetaRole to apply
roles to a class's metaclasses. This topic is covered at length in
various Moose::Cookbook recipes.
.SH "PROTOCOLS"
.IX Header "PROTOCOLS"
The meta-object protocol is divided into 4 main sub-protocols:
.SS "The Class protocol"
.IX Subsection "The Class protocol"
This provides a means of manipulating and introspecting a Perl 5
class. It handles symbol table hacking for you, and provides a rich
set of methods that go beyond simple package introspection.
.PP
See Class::MOP::Class for more details.
.SS "The Attribute protocol"
.IX Subsection "The Attribute protocol"
This provides a consistent representation for an attribute of a Perl 5
class. Since there are so many ways to create and handle attributes in
Perl 5 \s-1OO,\s0 the Attribute protocol provide as much of a unified
approach as possible. Of course, you are always free to extend this
protocol by subclassing the appropriate classes.
.PP
See Class::MOP::Attribute for more details.
.SS "The Method protocol"
.IX Subsection "The Method protocol"
This provides a means of manipulating and introspecting methods in the
Perl 5 object system. As with attributes, there are many ways to
approach this topic, so we try to keep it pretty basic, while still
making it possible to extend the system in many ways.
.PP
See Class::MOP::Method for more details.
.SS "The Instance protocol"
.IX Subsection "The Instance protocol"
This provides a layer of abstraction for creating object instances.
Since the other layers use this protocol, it is relatively easy to
change the type of your instances from the default hash reference to
some other type of reference. Several examples are provided in the
\&\fIexamples/\fR directory included in this distribution.
.PP
See Class::MOP::Instance for more details.
.SH "FUNCTIONS"
.IX Header "FUNCTIONS"
Note that this module does not export any constants or functions.
.SS "Utility functions"
.IX Subsection "Utility functions"
Note that these are all called as \fBfunctions, not methods\fR.
.PP
\fIClass::MOP::get_code_info($code)\fR
.IX Subsection "Class::MOP::get_code_info($code)"
.PP
This function returns two values, the name of the package the \f(CW$code\fR
is from and the name of the \f(CW$code\fR itself. This is used by several
elements of the \s-1MOP\s0 to determine where a given \f(CW$code\fR reference is
from.
.PP
\fIClass::MOP::class_of($instance_or_class_name)\fR
.IX Subsection "Class::MOP::class_of($instance_or_class_name)"
.PP
This will return the metaclass of the given instance or class name. If the
class lacks a metaclass, no metaclass will be initialized, and \f(CW\*(C`undef\*(C'\fR will be
returned.
.PP
You should almost certainly be using
\&\f(CW\*(C`Moose::Util::find_meta\*(C'\fR instead.
.SS "Metaclass cache functions"
.IX Subsection "Metaclass cache functions"
\&\f(CW\*(C`Class::MOP\*(C'\fR holds a cache of metaclasses. The following are functions
(\fBnot methods\fR) which can be used to access that cache. It is not
recommended that you mess with these. Bad things could happen, but if
you are brave and willing to risk it: go for it!
.PP
\fIClass::MOP::get_all_metaclasses\fR
.IX Subsection "Class::MOP::get_all_metaclasses"
.PP
This will return a hash of all the metaclass instances that have
been cached by Class::MOP::Class, keyed by the package name.
.PP
\fIClass::MOP::get_all_metaclass_instances\fR
.IX Subsection "Class::MOP::get_all_metaclass_instances"
.PP
This will return a list of all the metaclass instances that have
been cached by Class::MOP::Class.
.PP
\fIClass::MOP::get_all_metaclass_names\fR
.IX Subsection "Class::MOP::get_all_metaclass_names"
.PP
This will return a list of all the metaclass names that have
been cached by Class::MOP::Class.
.PP
\fIClass::MOP::get_metaclass_by_name($name)\fR
.IX Subsection "Class::MOP::get_metaclass_by_name($name)"
.PP
This will return a cached Class::MOP::Class instance, or nothing
if no metaclass exists with that \f(CW$name\fR.
.PP
\fIClass::MOP::store_metaclass_by_name($name, \f(CI$meta\fI)\fR
.IX Subsection "Class::MOP::store_metaclass_by_name($name, $meta)"
.PP
This will store a metaclass in the cache at the supplied \f(CW$key\fR.
.PP
\fIClass::MOP::weaken_metaclass($name)\fR
.IX Subsection "Class::MOP::weaken_metaclass($name)"
.PP
In rare cases (e.g. anonymous metaclasses) it is desirable to
store a weakened reference in the metaclass cache. This
function will weaken the reference to the metaclass stored
in \f(CW$name\fR.
.PP
\fIClass::MOP::metaclass_is_weak($name)\fR
.IX Subsection "Class::MOP::metaclass_is_weak($name)"
.PP
Returns true if the metaclass for \f(CW$name\fR has been weakened
(via \f(CW\*(C`weaken_metaclass\*(C'\fR).
.PP
\fIClass::MOP::does_metaclass_exist($name)\fR
.IX Subsection "Class::MOP::does_metaclass_exist($name)"
.PP
This will return true of there exists a metaclass stored in the
\&\f(CW$name\fR key, and return false otherwise.
.PP
\fIClass::MOP::remove_metaclass_by_name($name)\fR
.IX Subsection "Class::MOP::remove_metaclass_by_name($name)"
.PP
This will remove the metaclass stored in the \f(CW$name\fR key.
.PP
Some utility functions (such as \f(CW\*(C`Class::MOP::load_class\*(C'\fR) that were
previously defined in \f(CW\*(C`Class::MOP\*(C'\fR regarding loading of classes have been
extracted to Class::Load. Please see Class::Load for documentation.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
.SS "Books"
.IX Subsection "Books"
There are very few books out on Meta Object Protocols and Metaclasses
because it is such an esoteric topic. The following books are really
the only ones I have found. If you know of any more, \fB\f(BIplease\fB\fR
email me and let me know, I would love to hear about them.
.IP "\fIThe Art of the Meta Object Protocol\fR" 4
.IX Item "The Art of the Meta Object Protocol"
.PD 0
.IP "\fIAdvances in Object-Oriented Metalevel Architecture and Reflection\fR" 4
.IX Item "Advances in Object-Oriented Metalevel Architecture and Reflection"
.IP "\fIPutting MetaClasses to Work\fR" 4
.IX Item "Putting MetaClasses to Work"
.IP "\fISmalltalk: The Language\fR" 4
.IX Item "Smalltalk: The Language"
.PD
.SS "Papers"
.IX Subsection "Papers"
.ie n .IP """Uniform and safe metaclass composition""" 4
.el .IP "``Uniform and safe metaclass composition''" 4
.IX Item "Uniform and safe metaclass composition"
An excellent paper by the people who brought us the original Traits paper.
This paper is on how Traits can be used to do safe metaclass composition,
and offers an excellent introduction section which delves into the topic of
metaclass compatibility.
.Sp
<http://scg.unibe.ch/archive/papers/Duca05ySafeMetaclassTrait.pdf>
.ie n .IP """Safe Metaclass Programming""" 4
.el .IP "``Safe Metaclass Programming''" 4
.IX Item "Safe Metaclass Programming"
This paper seems to precede the above paper, and propose a mix-in based
approach as opposed to the Traits based approach. Both papers have similar
information on the metaclass compatibility problem space.
.Sp
<http://citeseer.ist.psu.edu/37617.html>
.SS "Prior Art"
.IX Subsection "Prior Art"
.IP "The Perl 6 MetaModel work in the Pugs project" 4
.IX Item "The Perl 6 MetaModel work in the Pugs project"
.RS 4
.PD 0
.IP "<http://github.com/perl6/p5\-modules/tree/master/Perl6\-ObjectSpace/>" 4
.IX Item "<http://github.com/perl6/p5-modules/tree/master/Perl6-ObjectSpace/>"
.RE
.RS 4
.RE
.PD
.SS "Articles"
.IX Subsection "Articles"
.IP "\s-1CPAN\s0 Module Review of Class::MOP" 4
.IX Item "CPAN Module Review of Class::MOP"
<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
.SH "SIMILAR MODULES"
.IX Header "SIMILAR MODULES"
As I have said above, this module is a class-builder-builder, so it is
not the same thing as modules like Class::Accessor and
Class::MethodMaker. That being said there are very few modules on \s-1CPAN\s0
with similar goals to this module. The one I have found which is most
like this module is Class::Meta, although its philosophy and the \s-1MOP\s0 it
creates are very different from this modules.
.SH "BUGS"
.IX Header "BUGS"
All complex software has bugs lurking in it, and this module is no
exception.
.PP
Please report any bugs to \f(CW\*(C`bug\-class\-mop@rt.cpan.org\*(C'\fR, or through the
web interface at <http://rt.cpan.org>.
.PP
You can also discuss feature requests or possible bugs on the Moose
mailing list (moose@perl.org) or on \s-1IRC\s0 at
<irc://irc.perl.org/#moose>.
.SH "ACKNOWLEDGEMENTS"
.IX Header "ACKNOWLEDGEMENTS"
.IP "Rob Kinyon" 4
.IX Item "Rob Kinyon"
Thanks to Rob for actually getting the development of this module kick-started.
.SH "AUTHORS"
.IX Header "AUTHORS"
.IP "\(bu" 4
Stevan Little <stevan@cpan.org>
.IP "\(bu" 4
Dave Rolsky <autarch@urth.org>
.IP "\(bu" 4
Jesse Luehrs <doy@cpan.org>
.IP "\(bu" 4
Shawn M Moore <sartak@cpan.org>
.IP "\(bu" 4
יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
.IP "\(bu" 4
Karen Etheridge <ether@cpan.org>
.IP "\(bu" 4
Florian Ragwitz <rafl@debian.org>
.IP "\(bu" 4
Hans Dieter Pearcey <hdp@cpan.org>
.IP "\(bu" 4
Chris Prather <chris@prather.org>
.IP "\(bu" 4
Matt S Trout <mstrout@cpan.org>
.SH "COPYRIGHT AND LICENSE"
.IX Header "COPYRIGHT AND LICENSE"
This software is copyright (c) 2006 by Infinity Interactive, Inc.
.PP
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.