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

VERSION

9       version 2.2014
10

INTRODUCTION

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

GETTING STARTED

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

USING THE METACLASS OBJECT

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

ALTERING CLASSES WITH THE MOP

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

GOING FURTHER

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

AUTHORS

148       •   Stevan Little <stevan@cpan.org>
149
150       •   Dave Rolsky <autarch@urth.org>
151
152       •   Jesse Luehrs <doy@cpan.org>
153
154       •   Shawn M Moore <sartak@cpan.org>
155
156       •   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
157
158       •   Karen Etheridge <ether@cpan.org>
159
160       •   Florian Ragwitz <rafl@debian.org>
161
162       •   Hans Dieter Pearcey <hdp@cpan.org>
163
164       •   Chris Prather <chris@prather.org>
165
166       •   Matt S Trout <mstrout@cpan.org>
167
169       This software is copyright (c) 2006 by Infinity Interactive, Inc.
170
171       This is free software; you can redistribute it and/or modify it under
172       the same terms as the Perl 5 programming language system itself.
173
174
175
176perl v5.32.1                      2021-01-27             Moose::Manual::MOP(3)
Impressum