1Moose::Manual::MOP(3) User Contributed Perl DocumentationMoose::Manual::MOP(3)
2
3
4

NAME

6       Moose::Manual::MOP - The Moose (and Class::MOP) meta API
7

INTRODUCTION

9       Moose provides a powerful introspection API built on top of
10       "Class::MOP". "MOP" stands for Meta-Object Protocol. In plainer
11       English, a MOP is an API for performing introspection on classes,
12       attributes, methods, and so on.
13
14       In fact, it is "Class::MOP" that provides many of Moose's core
15       features, including attributes, before/after/around method modifiers,
16       and immutability. In most cases, Moose takes an existing "Class::MOP"
17       class and subclasses it to add additional features. Moose also adds
18       some entirely new features of its own, such as roles, the augment
19       modifier, and types.
20
21       If you're interested in the MOP, it's important to know about
22       "Class::MOP" so you know what docs to read. Often, the introspection
23       method that you're looking for is defined in a "Class::MOP" class,
24       rather than Moose itself.
25
26       The MOP provides more than just read-only introspection. It also lets
27       you add attributes and methods, apply roles, and much more. In fact,
28       all of the declarative Moose sugar is simply a thin layer on top of the
29       MOP API.
30
31       If you want to write Moose extensions, you'll need to learn some of the
32       MOP API. The introspection methods are also handy if you want to
33       generate docs or inheritance graphs, or do some other runtime
34       reflection.
35
36       This document is not a complete reference for the meta API. We're just
37       going to cover some of the highlights, and give you a sense of how it
38       all works. To really understand it, you'll have to read a lot of other
39       docs, and possibly even dig into the Moose guts a bit.
40

GETTING STARTED

42       The usual entry point to the meta API is through a class's metaclass
43       object, which is a Moose::Meta::Class. This is available by calling the
44       "meta" method on a class or object:
45
46         package User;
47
48         use Moose;
49
50         my $meta = __PACKAGE__->meta;
51
52       The "meta" method is added to a class when it uses Moose.
53
54       You can also use "Class::MOP::Class->initialize($name)" to get a
55       metaclass object for any class. This is safer than calling
56       "$class->meta" when you're not sure that the class has a meta method.
57
58       The "Class::MOP::Class->initialize" constructor will return an existing
59       metaclass if one has already been created (via Moose or some other
60       means). If it hasn't, it will return a new "Class::MOP::Class" object.
61       This will work for classes that use Moose, meta API classes, and
62       classes which don't use Moose at all.
63

USING THE METACLASS OBJECT

65       The metaclass object can tell you about a class's attributes, methods,
66       roles, parents, and more. For example, to look at all of the class's
67       attributes:
68
69         for my $attr ( $meta->get_all_attributes ) {
70             print $attr->name, "\n";
71         }
72
73       The "get_all_attributes" method is documented in "Class::MOP::Class".
74       For Moose-using classes, it returns a list of Moose::Meta::Attribute
75       objects for attributes defined in the class and its parents.
76
77       You can also get a list of methods:
78
79         for my $method ( $meta->get_all_methods ) {
80             print $method->fully_qualified_name, "\n";
81         }
82
83       Now we're looping over a list of Moose::Meta::Method objects. Note that
84       some of these objects may actually be a subclass of
85       Moose::Meta::Method, as Moose uses different classes to represent
86       wrapped methods, delegation methods, constructors, etc.
87
88       We can look at a class's parent classes and subclasses:
89
90         for my $class ( $meta->linearized_isa ) {
91             print "$class\n";
92         }
93
94         for my $subclass ( $meta->subclasses ) {
95             print "$subclass\n";
96         }
97
98       Note that both these methods return class names, not metaclass objects.
99

ALTERING CLASSES WITH THE MOP

101       The metaclass object can change the class directly, by adding
102       attributes, methods, etc.
103
104       As an example, we can add a method to a class:
105
106         $meta->add_method( 'say' => sub { print @_, "\n" } );
107
108       Or an attribute:
109
110         $meta->add_attribute( 'size' => { is => 'rw', isa  => 'Int' } );
111
112       Obviously, this is much more cumbersome than using Perl syntax or Moose
113       sugar for defining methods and attributes, but this API allows for very
114       powerful extensions.
115
116       You might remember that we've talked about making classes immutable
117       elsewhere in the manual. This is a good practice. However, once a class
118       is immutable, calling any of these update methods will throw an
119       exception.
120
121       You can make a class mutable again simply by calling
122       "$meta->make_mutable". Once you're done changing it, you can restore
123       immutability by calling "$meta->make_immutable".
124
125       However, the most common use for this part of the meta API is as part
126       of Moose extensions. These extensions should assume that they are being
127       run before you make a class immutable.
128

GOING FURTHER

130       If you're interested in extending Moose, we recommend reading all of
131       the "Meta" and "Extending" recipes in the Moose::Cookbook. Those
132       recipes show various practical applications of the MOP.
133
134       If you'd like to write your own extensions, one of the best ways to
135       learn more about this is to look at other similar extensions to see how
136       they work. You'll probably also need to read various API docs,
137       including the docs for the various Moose::Meta::* classes and the
138       "Class::MOP" distribution.
139
140       Finally, we welcome questions on the Moose mailing list and IRC.
141       Information on the mailing list, IRC, and more references can be found
142       in the Moose.pm docs.
143

AUTHOR

145       Dave Rolsky <autarch@urth.org> and Stevan Little
146       <stevan@iinteractive.com>
147
149       Copyright 2009 by Infinity Interactive, Inc.
150
151       <http://www.iinteractive.com>
152
153       This library is free software; you can redistribute it and/or modify it
154       under the same terms as Perl itself.
155
156
157
158perl v5.12.2                      2010-08-21             Moose::Manual::MOP(3)
Impressum