1 package Moose::Manual::MOP;
3 # ABSTRACT: The Moose (and Class::MOP) meta API
11 Moose provides a powerful introspection API built on top of
12 C<Class::MOP>. "MOP" stands for Meta-Object Protocol. In plainer
13 English, a MOP is an API for performing introspection on classes,
14 attributes, methods, and so on.
16 In fact, it is C<Class::MOP> that provides many of Moose's core
17 features, including attributes, before/after/around method modifiers,
18 and immutability. In most cases, Moose takes an existing C<Class::MOP>
19 class and subclasses it to add additional features. Moose also adds
20 some entirely new features of its own, such as roles, the augment
23 If you're interested in the MOP, it's important to know about
24 C<Class::MOP> so you know what docs to read. Often, the introspection
25 method that you're looking for is defined in a C<Class::MOP> class,
26 rather than Moose itself.
28 The MOP provides more than just I<read-only> introspection. It also
29 lets you add attributes and methods, apply roles, and much more. In
30 fact, all of the declarative Moose sugar is simply a thin layer on top
33 If you want to write Moose extensions, you'll need to learn some of
34 the MOP API. The introspection methods are also handy if you want to
35 generate docs or inheritance graphs, or do some other runtime
38 This document is not a complete reference for the meta API. We're just
39 going to cover some of the highlights, and give you a sense of how it
40 all works. To really understand it, you'll have to read a lot of other
41 docs, and possibly even dig into the Moose guts a bit.
43 =head1 GETTING STARTED
45 The usual entry point to the meta API is through a class's metaclass
46 object, which is a L<Moose::Meta::Class>. This is available by calling
47 the C<meta> method on a class or object:
53 my $meta = __PACKAGE__->meta;
55 The C<meta> method is added to a class when it uses Moose.
57 You can also use C<< Class::MOP::Class->initialize($name) >> to get a
58 metaclass object for any class. This is safer than calling C<<
59 $class->meta >> when you're not sure that the class has a meta method.
61 The C<< Class::MOP::Class->initialize >> constructor will return an
62 existing metaclass if one has already been created (via Moose or some
63 other means). If it hasn't, it will return a new C<Class::MOP::Class>
64 object. This will work for classes that use Moose, meta API classes,
65 and classes which don't use Moose at all.
67 =head1 USING THE METACLASS OBJECT
69 The metaclass object can tell you about a class's attributes, methods,
70 roles, parents, and more. For example, to look at all of the class's
73 for my $attr ( $meta->get_all_attributes ) {
74 print $attr->name, "\n";
77 The C<get_all_attributes> method is documented in
78 C<Class::MOP::Class>. For Moose-using classes, it returns a list of
79 L<Moose::Meta::Attribute> objects for attributes defined in the class
82 You can also get a list of methods:
84 for my $method ( $meta->get_all_methods ) {
85 print $method->fully_qualified_name, "\n";
88 Now we're looping over a list of L<Moose::Meta::Method> objects. Note
89 that some of these objects may actually be a subclass of
90 L<Moose::Meta::Method>, as Moose uses different classes to represent
91 wrapped methods, delegation methods, constructors, etc.
93 We can look at a class's parent classes and subclasses:
95 for my $class ( $meta->linearized_isa ) {
99 for my $subclass ( $meta->subclasses ) {
103 Note that both these methods return class I<names>, not metaclass
106 =head1 ALTERING CLASSES WITH THE MOP
108 The metaclass object can change the class directly, by adding
109 attributes, methods, etc.
111 As an example, we can add a method to a class:
113 $meta->add_method( 'say' => sub { print @_, "\n" } );
117 $meta->add_attribute( 'size' => ( is => 'rw', isa => 'Int' ) );
119 Obviously, this is much more cumbersome than using Perl syntax or
120 Moose sugar for defining methods and attributes, but this API allows
121 for very powerful extensions.
123 You might remember that we've talked about making classes immutable
124 elsewhere in the manual. This is a good practice. However, once a
125 class is immutable, calling any of these update methods will throw an
128 You can make a class mutable again simply by calling C<<
129 $meta->make_mutable >>. Once you're done changing it, you can
130 restore immutability by calling C<< $meta->make_immutable >>.
132 However, the most common use for this part of the meta API is as
133 part of Moose extensions. These extensions should assume that they are
134 being run before you make a class immutable.
138 If you're interested in extending Moose, we recommend reading all of
139 the "Meta" and "Extending" recipes in the L<Moose::Cookbook>. Those
140 recipes show various practical applications of the MOP.
142 If you'd like to write your own extensions, one of the best ways to
143 learn more about this is to look at other similar extensions to see
144 how they work. You'll probably also need to read various API docs,
145 including the docs for the various C<Moose::Meta::*> and
146 C<Class::MOP::*> classes.
148 Finally, we welcome questions on the Moose mailing list and
149 IRC. Information on the mailing list, IRC, and more references can be
150 found in the L<Moose.pm docs|Moose/GETTING HELP>.