Starting work on revising the manual
[gitmo/Moose.git] / lib / Moose / Manual / Concepts.pod
CommitLineData
c7c1e74e 1=pod
cde84af5 2
3=head1 NAME
4
d67ce58f 5Moose::Manual::Concepts - Moose OO concepts
cde84af5 6
7=head1 MOOSE CONCEPTS (VS "OLD SCHOOL" Perl)
8
9In the past, you may not have thought too much about the difference
10between packages and classes, attributes and methods, constructors and
c460adf1 11methods, etc. With Moose, these are all conceptually separate,
12though under the hood they're implemented with plain old Perl.
8f765b9e 13
cb8ad101 14Our meta-object protocol (aka MOP) provides well-defined introspection
15features for each of those concepts, and Moose in turn provides
16distinct sugar for each of them. Moose also introduces additional
17concepts such as roles, method modifiers, and declarative delegation.
cde84af5 18
19Knowing what these concepts mean in Moose-speak, and how they used to
20be done in old school Perl 5 OO is a good way to start learning to use
21Moose.
22
23=head2 Class
24
8f765b9e 25When you say "use Moose" in a package, you are making your package a
26class. At its simplest, a class will consist simply of attributes
cde84af5 27and/or methods. It can also include roles, method modifiers, and more.
28
29A class I<has> zero or more B<attributes>.
30
31A class I<has> zero or more B<methods>.
32
33A class I<has> zero or more superclasses (aka parent classes). A
34class inherits from its superclass(es).
35
36A class I<has> zero or more B<method modifiers>. These modifiers can
37apply to its own methods or methods that are inherited from its
38ancestors.
39
cb8ad101 40A class I<does> (and I<consumes>) zero or more B<roles>.
cde84af5 41
42A class I<has> a B<constructor> and a B<destructor>. These are
43provided for you "for free" by Moose.
44
45The B<constructor> accepts named parameters corresponding to the
46class's attributes and uses them to initialize an B<object instance>.
47
48A class I<has> a B<metaclass>, which in turn has B<meta-attributes>,
49B<meta-methods>, and B<meta-roles>. This metaclass I<describes> the
50class.
51
52A class is usually analogous to a category of nouns, like "People" or
53"Users".
54
55 package Person;
56
57 use Moose;
58 # now it's a Moose class!
59
60=head2 Attribute
61
62An attribute is a property of the class that defines it. It I<always>
cb8ad101 63has a name, and it I<may have> a number of other properties.
cde84af5 64
cb8ad101 65These properties can include a read/write flag, a B<type>, accessor
66method names, B<delegations>, a default value, and more.
cde84af5 67
68Attributes I<are not> methods, but defining them causes various
69accessor methods to be created. At a minimum, a normal attribute will
c460adf1 70have a reader accessor method. Many attributes have other
71methods, such as a writer method, a clearer method, or a predicate method
cb8ad101 72("has it been set?").
cde84af5 73
cb8ad101 74An attribute may also define B<delegations>, which will create
75additional methods based on the delegation mapping.
cde84af5 76
77By default, Moose stores attributes in the object instance, which is a
cb8ad101 78hashref, I<but this is invisible to the author of a Moose-based
79class>! It is best to think of Moose attributes as "properties" of
80the I<opaque> B<object instance>. These properties are accessed
81through well-defined accessor methods.
cde84af5 82
8f765b9e 83An attribute is something that the class's members have. For example,
84People have first and last names. Users have passwords and last login
85datetimes.
cde84af5 86
87 has 'first_name' => (
88 is => 'rw',
89 isa => 'Str',
90 );
91
92=head2 Method
93
cb8ad101 94A B<method> is very straightforward. Any subroutine you define in your
cde84af5 95class is a method.
96
cb8ad101 97B<Methods> correspond to verbs, and are what your objects can do. For
cde84af5 98example, a User can login.
99
100 sub login { ... }
101
c460adf1 102=head2 Role
cde84af5 103
cb8ad101 104A role is something that a class I<does>. We also say that classes
105I<consume> roles. For example, a Machine class might do the Breakable
106role, and so could a Bone class. A role is used to define some concept
107that cuts across multiple unrelated classes, like "breakability", or
108"has a color".
cde84af5 109
110A role I<has> zero or more B<attributes>.
111
112A role I<has> zero or more B<methods>.
113
114A role I<has> zero or more B<method modifiers>.
115
116A role I<has> zero or more B<required methods>.
117
c460adf1 118A required method is not implemented by the role. Required methods are a way
119for the role to declare "to use this role you must implement this method".
cde84af5 120
cb8ad101 121A role I<has> zero or more B<excluded roles>.
122
123An excluded role is a role that the role doing the excluding says it
124cannot be combined with.
125
cde84af5 126Roles are I<composed> into classes (or other roles). When a role is
127composed into a class, its attributes and methods are "flattened" into
128the class. Roles I<do not> show up in the inheritance hierarchy. When
129a role is composed, its attributes and methods appear as if they were
130defined I<in the consuming class>.
131
132Role are somewhat like mixins or interfaces in other OO languages.
133
134 package Breakable;
135
136 use Moose::Role;
137
76127c77 138 requires 'break';
139
140 has 'is_broken' => (
cde84af5 141 is => 'rw',
142 isa => 'Bool',
143 );
144
76127c77 145 after 'break' => sub {
cde84af5 146 my $self = shift;
147
148 $self->is_broken(1);
149 };
150
d67ce58f 151=head2 Method modifiers
cde84af5 152
cb8ad101 153A B<method modifier> is a hook that is called when a named method is
8f765b9e 154called. For example, you could say "before calling C<login()>, call
155this modifier first". Modifiers come in different flavors like
156"before", "after", "around", and "augment", and you can apply more
157than one modifier to a single method.
cde84af5 158
159Method modifiers are often used as an alternative to overriding a
160method in a parent class. They are also used in roles as a way of
161modifying methods in the consuming class.
162
163Under the hood, a method modifier is just a plain old Perl subroutine
164that gets called before or after (or around, etc.) some named method.
165
166 before 'login' => sub {
167 my $self = shift;
168 my $pw = shift;
169
170 warn "Called login() with $pw\n";
171 };
172
173=head2 Type
174
c460adf1 175Moose also comes with a (miniature) type system. This allows you to define
176types for attributes. Moose has a set of built-in types based on the types
177Perl provides in its core, such as C<Str>, C<Num>, C<Bool>, C<HashRef>, etc.
cde84af5 178
179In addition, every class name in your application can also be used as
dab94063 180a type name.
cde84af5 181
c460adf1 182Finally, you can define your own types with their own constraints. For
183example, you could define a C<PosInt> type, a subtype of C<Int> which only
184allows positive numbers.
cde84af5 185
186=head2 Delegation
187
c460adf1 188Moose attributes provide declarative syntax for defining delegations. A
189delegation is a method which in turn calls some method on an attribute to do
190its real work.
cde84af5 191
192=head2 Constructor
193
194A constructor creates an B<object instance> for the class. In old
195school Perl, this was usually done by defining a method called
196C<new()> which in turn called C<bless> on a reference.
197
198With Moose, this C<new()> method is created for you, and it simply
199does the right thing. You should never need to define your own
200constructor!
201
202Sometimes you want to do something whenever an object is created. In
203those cases, you can provide a C<BUILD()> method in your class. Moose
204will call this for you after creating a new object.
205
206=head2 Destructor
207
208This is a special method called when an object instance goes out of
209scope. You can specialize what your class does in this method if you
210need to, but you usually don't.
211
212With old school Perl 5, this is the C<DESTROY()> method, but with
213Moose it is the C<DEMOLISH()> method.
214
d67ce58f 215=head2 Object instance
cde84af5 216
217An object instance is a specific noun in the class's "category". For
218example, one specific Person or User. An instance is created by the
219class's B<constructor>.
220
221An instance has values for its attributes. For example, a specific
222person has a first and last name.
223
224In old school Perl 5, this is often a blessed hash reference. With
225Moose, you should never need to know what your object instance
6549b0d1 226actually is. (Okay, it's usually a blessed hashref with Moose, too.)
cde84af5 227
d67ce58f 228=head2 Moose vs old school summary
cde84af5 229
230=over 4
231
232=item * Class
233
234A package with no introspection other than mucking about in the symbol
235table.
236
237With Moose, you get well-defined declaration and introspection.
238
239=item * Attributes
240
241Hand-written accessor methods, symbol table hackery, or a helper
242module like C<Class::Accessor>.
243
244With Moose, these are declaratively defined, and distinct from
245methods.
246
247=item * Method
248
249These are pretty much the same in Moose as in old school Perl.
250
251=item * Roles
252
253C<Class::Trait> or C<Class::Role>, or maybe C<mixin.pm>.
254
255With Moose, they're part of the core feature set, and are
256introspectable like everything else.
257
258=item * Method Modifiers
259
260Could only be done through serious symbol table wizardry, and you
261probably never saw this before (at least in Perl 5).
262
263=item * Type
264
265Hand-written parameter checking in your C<new()> method and accessors.
266
267With Moose, you define types declaratively, and then use them by name
c460adf1 268with your attributes.
cde84af5 269
270=item * Delegation
271
272C<Class::Delegation> or C<Class::Delegator>, but probably even more
273hand-written code.
274
275With Moose, this is also declarative.
276
277=item * Constructor
278
279A C<new()> method which calls C<bless> on a reference.
280
281Comes for free when you define a class with Moose.
282
283=item * Destructor
284
285A C<DESTROY()> method.
286
287With Moose, this is called C<DEMOLISH()>.
288
289=item * Object Instance
290
291A blessed reference, usually a hash reference.
292
293With Moose, this is an opaque thing which has a bunch of attributes
294and methods, as defined by its class.
295
296=item * Immutabilization
297
298Moose comes with a feature called "immutabilization". When you make
299your class immutable, it means you're done adding methods, attributes,
300roles, etc. This lets Moose optimize your class with a bunch of
301extremely dirty in-place code generation tricks that speed up things
302like object construction and so on.
303
304=back
305
306=head1 META WHAT?
307
c460adf1 308A metaclass is a class that describes classes. With Moose, every class you
309define gets a C<meta()> method. The C<meta()> method returns a
310L<Moose::Meta::Class> object, which has an introspection API that can tell you
311about the class it represents.
cde84af5 312
313 my $meta = User->meta();
314
b2df9268 315 for my $attribute ( $meta->get_all_attributes ) {
cde84af5 316 print $attribute->name(), "\n";
317
318 if ( $attribute->has_type_constraint ) {
319 print " type: ", $attribute->type_constraint->name, "\n";
320 }
321 }
322
b2df9268 323 for my $method ( $meta->get_all_methods ) {
cde84af5 324 print $method->name, "\n";
325 }
326
327Almost every concept we defined earlier has a meta class, so we have
328L<Moose::Meta::Class>, L<Moose::Meta::Attribute>,
329L<Moose::Meta::Method>, L<Moose::Meta::Role>,
330L<Moose::Meta::TypeConstraint>, L<Moose::Meta::Instance>, and so on.
331
332=head1 BUT I NEED TO DO IT MY WAY!
333
334One of the great things about Moose is that if you dig down and find
335that it does something the "wrong way", you can change it by extending
336a metaclass. For example, you can have arrayref based objects, you can
6549b0d1 337make your constructors strict (no unknown parameters allowed!), you can
cde84af5 338define a naming scheme for attribute accessors, you can make a class a
339Singleton, and much, much more.
340
341Many of these extensions require surprisingly small amounts of code,
342and once you've done it once, you'll never have to hand-code "your way
343of doing things" again. Instead you'll just load your favorite
344extensions.
345
346 package MyWay::User;
347
348 use Moose;
349 use MooseX::StrictConstructor
350 use MooseX::MyWay;
351
352 has ...;
353
354=head1 WHAT NEXT?
355
356So you're sold on Moose. Time to learn how to really use it.
357
dab94063 358If you want to see how Moose would translate directly into old school
d68b26d1 359Perl 5 OO code, check out L<Moose::Manual::Unsweetened>. This might be
dab94063 360helpful for quickly wrapping your brain around some aspects of "the
361Moose way".
cde84af5 362
d68b26d1 363Or you can skip that and jump straight to L<Moose::Manual::Classes>
364and the rest of the L<Moose::Manual>.
cde84af5 365
366After that we recommend that you start with the L<Moose::Cookbook>. If
367you work your way through all the recipes under the basics section,
368you should have a pretty good sense of how Moose works, and all of its
369basic OO features.
370
371After that, check out the Role recipes. If you're really curious, go
372on and read the Meta and Extending recipes, but those are mostly there
c460adf1 373for people who want to be Moose wizards and extend Moose itself.
cde84af5 374
375=head1 AUTHOR
376
377Dave Rolsky E<lt>autarch@urth.orgE<gt>
378
379=head1 COPYRIGHT AND LICENSE
380
cb8ad101 381Copyright 2008-2009 by Infinity Interactive, Inc.
cde84af5 382
383L<http://www.iinteractive.com>
384
385This library is free software; you can redistribute it and/or modify
386it under the same terms as Perl itself.
387
388=cut