Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Moose / Manual / Concepts.pod
CommitLineData
3fea05b9 1
2
3=head1 NAME
4
5Moose::Manual::Concepts - Moose OO concepts
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
11methods, etc. With Moose, these are all conceptually separate things,
12even though under the hood they're implemented with plain old Perl.
13
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.
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
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
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
40A class I<does> (and I<consumes>) zero or more B<roles>.
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>
63has a name, and it I<may have> a number of other properties.
64
65These properties can include a read/write flag, a B<type>, accessor
66method names, B<delegations>, a default value, and more.
67
68Attributes I<are not> methods, but defining them causes various
69accessor methods to be created. At a minimum, a normal attribute will
70always have a reader accessor method. Many attributes also have other
71methods, such as a writer method, clearer method, and predicate method
72("has it been set?").
73
74An attribute may also define B<delegations>, which will create
75additional methods based on the delegation mapping.
76
77By default, Moose stores attributes in the object instance, which is a
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.
82
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.
86
87 has 'first_name' => (
88 is => 'rw',
89 isa => 'Str',
90 );
91
92=head2 Method
93
94A B<method> is very straightforward. Any subroutine you define in your
95class is a method.
96
97B<Methods> correspond to verbs, and are what your objects can do. For
98example, a User can login.
99
100 sub login { ... }
101
102=head2 Roles
103
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".
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
118A required method is not implemented by the role. Required methods say
119"to use this Role you must implement this method".
120
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
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
138 has is_broken => (
139 is => 'rw',
140 isa => 'Bool',
141 );
142
143 requires 'break';
144
145 before 'break' => {
146 my $self = shift;
147
148 $self->is_broken(1);
149 };
150
151=head2 Method modifiers
152
153A B<method modifier> is a hook that is called when a named method is
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.
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
175Moose also comes with a (miniature) type system. This allows you to
176define types for attributes. Moose has a set of built-in types based
177on what Perl provides, such as C<Str>, C<Num>, C<Bool>, C<HashRef>, etc.
178
179In addition, every class name in your application can also be used as
180a type name.
181
182Finally, you can define your own types, either as subtypes or entirely
183new types, with their own constraints. For example, you could define a
184type C<PosInt>, a subtype of C<Int> which only allows positive numbers.
185
186=head2 Delegation
187
188Moose attributes provide declarative syntax for defining
189delegations. A delegation is a method which calls some method on an
190attribute to do its real work.
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
215=head2 Object instance
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
226actually is. (Okay, it's usually a blessed hashref with Moose, too.)
227
228=head2 Moose vs old school summary
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
268in your attributes.
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
308A metaclass is a class that describes classes. With Moose, every class
309you define gets a C<meta()> method. It returns a L<Moose::Meta::Class>
310object, which has an introspection API that can tell you about the
311class it represents.
312
313 my $meta = User->meta();
314
315 for my $attribute ( $meta->get_all_attributes ) {
316 print $attribute->name(), "\n";
317
318 if ( $attribute->has_type_constraint ) {
319 print " type: ", $attribute->type_constraint->name, "\n";
320 }
321 }
322
323 for my $method ( $meta->get_all_methods ) {
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
337make your constructors strict (no unknown parameters allowed!), you can
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
358If you want to see how Moose would translate directly into old school
359Perl 5 OO code, check out L<Moose::Manual::Unsweetened>. This might be
360helpful for quickly wrapping your brain around some aspects of "the
361Moose way".
362
363Or you can skip that and jump straight to L<Moose::Manual::Classes>
364and the rest of the L<Moose::Manual>.
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
373for people who want to be Moose wizards and change how Moose works.
374
375=head1 AUTHOR
376
377Dave Rolsky E<lt>autarch@urth.orgE<gt>
378
379=head1 COPYRIGHT AND LICENSE
380
381Copyright 2008-2009 by Infinity Interactive, Inc.
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