Clarify difference between trigger & after method modifier
[gitmo/Moose.git] / lib / Moose / Manual / MOP.pod
CommitLineData
87817dcf 1=pod
2
3=head1 NAME
4
5Moose::Manual::MOP - The Moose (and Class::MOP) Meta API
6
7=head1 INTRODUCTION
8
967e8591 9Moose provides a powerful introspection API built on top of
87817dcf 10C<Class::MOP>. "MOP" stands for Meta-Object Protocol. In plainer
636f25f3 11English, a MOP is an API for performing introspection on classes,
967e8591 12attributes, methods, and so on.
87817dcf 13
14In fact, it is C<Class::MOP> that provides many of Moose's core
15features, including attributes, before/after/around method modifiers,
967e8591 16and immutability. In most cases, Moose takes an existing C<Class::MOP>
17class and subclasses it to add additional features. Moose also adds
18some entirely new features of its own, such as roles, the augment
19modifier, and types.
87817dcf 20
967e8591 21If you're interested in the MOP, it's important to know about
22C<Class::MOP> so you know what docs to read. Often, the introspection
23method that you're looking for is defined in a C<Class::MOP> class,
24rather than Moose itself.
87817dcf 25
26The MOP provides more than just I<read-only> introspection. It also
27lets you add attributes, method, apply roles, and much more. In fact,
28all of the declarative Moose sugar is simply a thin layer on top of
29the MOP API.
30
967e8591 31If you want to write Moose extensions, you'll need to learn some of
32the MOP API. The introspection methods are also handy if you want to
33generate docs or inheritance graphs, or do some other runtime
87817dcf 34reflection.
35
36This document is not a complete reference for the meta API. We're just
37going to cover some of the highlights, and give you a sense of how it
38all works. To really understand it, you'll have to read a lot of other
39docs, and possibly even dig into the Moose guts a bit.
40
41=head1 GETTING STARTED
42
967e8591 43The usual entry point to the meta API is through a class's metaclass
44object, which is a L<Moose::Meta::Class>. This is available by calling
45the C<meta> method on a class or object:
87817dcf 46
47 package User;
48
49 use Moose;
50
51 my $meta = __PACKAGE__->meta;
52
53The C<meta> method is added to a class when it uses Moose.
54
967e8591 55You can also use C<< Class::MOP::Class->initialize($name) >> to get a
87817dcf 56metaclass object for any class. This is safer than calling C<<
57$class->meta >> when you're not sure that the class has a meta method.
58
967e8591 59The C<< Class::MOP::Class->initialize >> constructor will return an
60existing metaclass if one has already been created (via Moose or some
61other means). If it hasn't, it will return a new C<Class::MOP::Class>
87817dcf 62object. This will work for classes that use Moose, meta API classes,
63and classes which don't use Moose at all.
64
65=head1 USING THE METACLASS OBJECT
66
67The metaclass object can tell you about a class's attributes, methods,
68roles, parents, and more. For example, to look at all of the class's
69attributes:
70
71 for my $attr ( $meta->get_all_attributes ) {
72 print $attr->name, "\n";
73 }
74
75The C<get_all_attributes> method is documented in
967e8591 76C<Class::MOP::Class>. For Moose-using classes, it returns a list of
77L<Moose::Meta::Attribute> objects for attributes defined in the class
78and its parents.
87817dcf 79
80You can also get a list of methods:
81
82 for my $method ( $meta->get_all_methods ) {
83 print $meth->fully_qualified_name, "\n";
84 }
85
86Now we're looping over a list of L<Moose::Meta::Method> objects. Note
87that some of these objects may actually be a subclass of
88L<Moose::Meta::Method>, as Moose uses different classes to represent
89wrapped methods, delegation methods, constructors, etc.
90
91We 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
101Note that both these methods return class I<names>, not metaclass
102objects.
103
967e8591 104=head1 ALTERING CLASSES WITH THE MOP
87817dcf 105
967e8591 106The metaclass object can change the class directly, by adding
107attributes, methods, etc.
87817dcf 108
109As an example, we can add a method to a class:
110
111 $meta->add_method( 'say' => sub { print @_, "\n" } );
112
113Or an attribute:
114
115 $meta->add_attribute(
116 name => 'size',
117 is => 'rw',
118 isa => 'Int',
119 );
120
967e8591 121Obviously, this is much more cumbersome than using Perl syntax or
122Moose sugar for defining methods and attributes, but this API allows
123for very powerful extensions.
124
125You might remember that we've talked about making classes immutable
126elsewhere in the manual. This is a good practice. However, once a
127class is immutable, calling any of these update methods will throw an
128exception.
129
130You can make a class mutable again simply by calling C<<
131$metaclass->make_mutable >>. Once you're done changing it, you can
132restore immutability by calling C<< $metaclass->make_immutable >>.
133
134However, the most common use for this part of of the meta API is as
135part of Moose extensions. These extensions should assume that they are
136being run before you make a class immutable.
87817dcf 137
138=head1 GOING FURTHER
139
967e8591 140If you're interested in extending moose, we recommend reading all of
141the "Meta" and "Extending" recipes in the L<Moose::Cookbook>. Those
142recipes show various practical applications of the MOP.
87817dcf 143
144If you'd like to write your own extensions, one of the best ways to
145learn more about this is to look at other similar extensions to see
146how they work. You'll probably also need to read various API docs,
147including the docs for the various Moose::Meta::* classes and the
148C<Class::MOP> distribution.
149
150Finally, we welcome questions on the Moose mailing list and
151IRC. Information on the mailing list, IRC, and more references can be
aa2c4ca6 152found in the L<Moose.pm docs|Moose/GETTING HELP>.
87817dcf 153
154=head1 AUTHOR
155
156Dave Rolsky E<lt>autarch@urth.orgE<gt> and Stevan Little
157E<lt>stevan@iinteractive.comE<gt>
158
159=head1 COPYRIGHT AND LICENSE
160
2840a3b2 161Copyright 2009 by Infinity Interactive, Inc.
87817dcf 162
163L<http://www.iinteractive.com>
164
165This library is free software; you can redistribute it and/or modify
166it under the same terms as Perl itself.
167
168=cut