1af6985e45414db06720b768227cfe1fd01b1f5d
[gitmo/Moose.git] / lib / Moose / Manual / MOP.pod
1 =pod
2
3 =head1 NAME
4
5 Moose::Manual::MOP - The Moose (and Class::MOP) meta API
6
7 =head1 INTRODUCTION
8
9 Moose provides a powerful introspection API built on top of
10 C<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 C<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 C<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 C<Class::MOP> so you know what docs to read. Often, the introspection
23 method that you're looking for is defined in a C<Class::MOP> class,
24 rather than Moose itself.
25
26 The MOP provides more than just I<read-only> introspection. It also
27 lets you add attributes and methods, apply roles, and much more. In
28 fact, all of the declarative Moose sugar is simply a thin layer on top
29 of the MOP API.
30
31 If you want to write Moose extensions, you'll need to learn some of
32 the 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
41 =head1 GETTING STARTED
42
43 The usual entry point to the meta API is through a class's metaclass
44 object, which is a L<Moose::Meta::Class>. This is available by calling
45 the C<meta> method on a class or object:
46
47   package User;
48
49   use Moose;
50
51   my $meta = __PACKAGE__->meta;
52
53 The C<meta> method is added to a class when it uses Moose.
54
55 You can also use C<< Class::MOP::Class->initialize($name) >> to get a
56 metaclass 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
59 The C<< Class::MOP::Class->initialize >> constructor will return an
60 existing metaclass if one has already been created (via Moose or some
61 other means). If it hasn't, it will return a new C<Class::MOP::Class>
62 object. This will work for classes that use Moose, meta API classes,
63 and classes which don't use Moose at all.
64
65 =head1 USING THE METACLASS OBJECT
66
67 The metaclass object can tell you about a class's attributes, methods,
68 roles, parents, and more. For example, to look at all of the class's
69 attributes:
70
71   for my $attr ( $meta->get_all_attributes ) {
72       print $attr->name, "\n";
73   }
74
75 The C<get_all_attributes> method is documented in
76 C<Class::MOP::Class>. For Moose-using classes, it returns a list of
77 L<Moose::Meta::Attribute> objects for attributes defined in the class
78 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 L<Moose::Meta::Method> objects. Note
87 that some of these objects may actually be a subclass of
88 L<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 I<names>, not metaclass
102 objects.
103
104 =head1 ALTERING CLASSES WITH THE MOP
105
106 The metaclass object can change the class directly, by adding
107 attributes, methods, etc.
108
109 As an example, we can add a method to a class:
110
111   $meta->add_method( 'say' => sub { print @_, "\n" } );
112
113 Or an attribute:
114
115   $meta->add_attribute( 'size' => ( is => 'rw', isa  => 'Int' ) );
116
117 Obviously, this is much more cumbersome than using Perl syntax or
118 Moose sugar for defining methods and attributes, but this API allows
119 for very powerful extensions.
120
121 You might remember that we've talked about making classes immutable
122 elsewhere in the manual. This is a good practice. However, once a
123 class is immutable, calling any of these update methods will throw an
124 exception.
125
126 You can make a class mutable again simply by calling C<<
127 $meta->make_mutable >>. Once you're done changing it, you can
128 restore immutability by calling C<< $meta->make_immutable >>.
129
130 However, the most common use for this part of the meta API is as
131 part of Moose extensions. These extensions should assume that they are
132 being run before you make a class immutable.
133
134 =head1 GOING FURTHER
135
136 If you're interested in extending Moose, we recommend reading all of
137 the "Meta" and "Extending" recipes in the L<Moose::Cookbook>. Those
138 recipes show various practical applications of the MOP.
139
140 If you'd like to write your own extensions, one of the best ways to
141 learn more about this is to look at other similar extensions to see
142 how they work. You'll probably also need to read various API docs,
143 including the docs for the various Moose::Meta::* classes and the
144 C<Class::MOP> distribution.
145
146 Finally, we welcome questions on the Moose mailing list and
147 IRC. Information on the mailing list, IRC, and more references can be
148 found in the L<Moose.pm docs|Moose/GETTING HELP>.
149
150 =head1 AUTHOR
151
152 Dave Rolsky E<lt>autarch@urth.orgE<gt> and Stevan Little
153 E<lt>stevan@iinteractive.comE<gt>
154
155 =head1 COPYRIGHT AND LICENSE
156
157 Copyright 2009 by Infinity Interactive, Inc.
158
159 L<http://www.iinteractive.com>
160
161 This library is free software; you can redistribute it and/or modify
162 it under the same terms as Perl itself.
163
164 =cut