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