move Unsweetened into the Manual
[gitmo/Moose.git] / lib / Moose / Manual.pod
CommitLineData
270df362 1=pod
2
3=head1 NAME
4
cde84af5 5Moose::Manual - What is Moose, and how do I use it?
270df362 6
ed5b20d9 7=head1 WHAT IS MOOSE?
270df362 8
ed5b20d9 9Moose is a I<complete> object system for Perl 5. Consider any modern
10object-oriented language (which Perl 5 definitely isn't). It provides
11keywords for attribute declaration, object construction, inheritance,
12and maybe more. These keywords are part of the language, and you don't
13care how they are implemented.
270df362 14
15Moose aims to do the same thing for Perl 5 OO. We can't actually
10821858 16create new keywords, but we do offer "sugar" that looks a lot like
ed5b20d9 17them. More importantly, with Moose, you I<define your class
18declaratively>, without needing to know about blessed hashrefs,
19accessor methods, and so on.
270df362 20
ed5b20d9 21With Moose, you can concentrate on the I<logical> structure of your
22classes, focusing on "what" rather than "how". A class definition with
23Moose reads like a list of very concise English sentences.
270df362 24
dab94063 25Moose is built on top of C<Class::MOP>, a meta-object protocol (aka
270df362 26MOP). Using the MOP, Moose provides complete introspection for all
27Moose-using classes. This means you can ask classes about their
28attributes, parents, children, methods, etc., all using a well-defined
ed5b20d9 29API. The MOP abstracts away the symbol table, looking at C<@ISA> vars,
30and all the other crufty Perl tricks we know and love(?).
270df362 31
10821858 32Moose is based in large part on the Perl 6 object system, as well as
33drawing on the best ideas from CLOS, Smalltalk, and many other
34languages.
270df362 35
36=head1 WHY MOOSE?
37
10821858 38Moose makes Perl 5 OO both simpler and more powerful. It encapsulates
ed5b20d9 39Perl 5 power tools in high-level declarative APIs which are easy to
40use. Best of all, you don't need to be a wizard to use it.
270df362 41
ed5b20d9 42But if you want to dig about in the guts, Moose lets you do that too,
43by using and extending its powerful introspection API.
270df362 44
45=head1 AN EXAMPLE
46
47 package Person;
48
49 use Moose;
50
51 has 'first_name' => (
52 is => 'rw',
53 isa => 'Str',
54 );
55
56 has 'last_name' => (
57 is => 'rw',
58 isa => 'Str',
59 );
60
0079cb0c 61 no Moose;
62 __PACKAGE__->meta->make_immutable;
63
270df362 64This is a I<complete and usable> class definition!
65
66 package User;
67
68 use DateTime;
69 use Moose;
70
71 extends 'Person';
72
73 has 'password' => (
74 is => 'rw',
f330de33 75 isa => 'Str',
76 );
270df362 77
78 has 'last_login' => (
79 is => 'rw',
80 isa => 'DateTime',
fec9ca3e 81 handles => { 'date_of_last_login' => 'date' },
270df362 82 );
83
84 sub login {
85 my $self = shift;
86 my $pw = shift;
87
88 return 0 if $pw ne $self->password;
89
90 $self->last_login( DateTime->now() );
91
92 return 1;
93 }
94
0079cb0c 95 no Moose;
96 __PACKAGE__->meta->make_immutable;
97
270df362 98We'll leave the line-by-line explanation of this code to other
10821858 99documentation, but you can see how Moose reduces common OO idioms to
100simple declarative constructs.
270df362 101
cde84af5 102=head2 TABLE OF CONTENTS
270df362 103
cde84af5 104This manual consists of a number of documents.
270df362 105
106=over 4
107
cde84af5 108=item L<Moose::Manual::Concepts>
270df362 109
cde84af5 110Introduces Moose concepts, and contrasts them against "old school"
111Perl 5 OO.
270df362 112
d68b26d1 113=item L<Moose::Manual::Unsweetened>
114
115Show two examples classes, each written first with Moose and then with
116"plain old Perl 5".
117
cdebe1c6 118=item L<Moose::Manual::Classes>
270df362 119
cdebe1c6 120How do you make use of Moose in your classes? Now that I'm a Moose,
121how do I subclass something?
270df362 122
cdebe1c6 123=item L<Moose::Manual::Attributes>
270df362 124
cdebe1c6 125Attributes are a core part of the Moose OO system. An attribute is a
126piece of data that an object has. Moose has a lot of attribute-related
127features!
270df362 128
cef2dfda 129=item L<Moose::Manual::Delegation>
130
dab94063 131Delegation is a powerful way to make use of attributes which are
cef2dfda 132themselves objects.
133
cde84af5 134=item L<Moose::Manual::Construction>
270df362 135
cde84af5 136Learn how objects are built in Moose, and in particular about the
0e27121a 137C<BUILD> and C<BUILDARGS> methods. Also covers object destruction
cde84af5 138with C<DEMOLISH>.
270df362 139
cde84af5 140=item L<Moose::Manual::MethodModifiers>
270df362 141
cde84af5 142A method modifier lets you say "before calling method X, do this
143first", or "wrap method X in this code". Method modifiers are
144particularly handy in roles and with attribute accessors.
270df362 145
cde84af5 146=item L<Moose::Manual::Roles>
270df362 147
cde84af5 148A role is something a class does (like "Debuggable" or
149"Printable"). Roles provide a way of adding behavior to classes that
150is orthogonal to inheritance.
270df362 151
cde84af5 152=item L<Moose::Manual::Types>
270df362 153
cde84af5 154Moose's type system lets you strictly define what values an attribute
155can contain.
270df362 156
87817dcf 157=item L<Moose::Manual::MOP>
270df362 158
87817dcf 159Moose's meta API system lets you ask classes about their parents,
160children, methods, attributes, etc.
270df362 161
cde84af5 162=item L<Moose::Manual::MooseX>
270df362 163
dab94063 164This document describes a few of the most useful Moose extensions on
165CPAN.
e03c5ed2 166
0e27121a 167=item L<Moose::Manual::BestPractices>
168
169Moose has a lot of features, and there's definitely more than one way
170to do it. However, we think that picking a subset of these features
171and using them consistently makes everyone's life easier.
172
29d05a51 173=item L<Moose::Manual::Contributing>
174
175Interested in hacking on Moose? Read this.
176
177=item L<Moose::Manual::Delta>
178
179This document details backwards-incompatibilities and other major
180changes to Moose.
181
270df362 182=back
183
270df362 184=head1 JUSTIFICATION
185
dab94063 186If you're still asking yourself "Why do I need this?", then this
270df362 187section is for you.
188
189=over 4
190
191=item Another object system!?!?
192
ed5b20d9 193Yes, we know there are many, many ways to build objects in Perl 5,
194many of them based on inside-out objects and other such things. Moose
195is different because it is not a new object system for Perl 5, but
c56e5db4 196instead an extension of the existing object system.
270df362 197
198Moose is built on top of L<Class::MOP>, which is a metaclass system
199for Perl 5. This means that Moose not only makes building normal
200Perl 5 objects better, but it also provides the power of metaclass
201programming.
202
203=item Is this for real? Or is this just an experiment?
204
205Moose is I<based> on the prototypes and experiments Stevan did for the
206Perl 6 meta-model. However, Moose is B<NOT> an experiment or
207prototype; it is for B<real>.
208
209=item Is this ready for use in production?
210
211Yes.
212
c56e5db4 213Moose has been used successfully in production environments by many
270df362 214people and companies. There are Moose applications which have been in
0e27121a 215production with little or no issue now for years. We consider it
216highly stable and we are committed to keeping it stable.
270df362 217
218Of course, in the end, you need to make this call yourself. If you
dab94063 219have any questions or concerns, please feel free to email Stevan or
220the moose@perl.org list, or just stop by irc.perl.org#moose and ask
221away.
270df362 222
223=item Is Moose just Perl 6 in Perl 5?
224
225No. While Moose is very much inspired by Perl 6, it is not itself Perl
0e27121a 2266. Instead, it is an OO system for Perl 5. Stevan built Moose because
270df362 227he was tired of writing the same old boring Perl 5 OO code, and
228drooling over Perl 6 OO. So instead of switching to Ruby, he wrote
229Moose :)
230
231=item Wait, I<post> modern, I thought it was just I<modern>?
232
233Stevan read Larry Wall's talk from the 1999 Linux World entitled
234"Perl, the first postmodern computer language" in which he talks about
235how he picked the features for Perl because he thought they were cool
236and he threw out the ones that he thought sucked. This got him
237thinking about how we have done the same thing in Moose. For Moose, we
238have "borrowed" features from Perl 6, CLOS (LISP), Smalltalk, Java,
239BETA, OCaml, Ruby and more, and the bits we didn't like (cause they
240sucked) we tossed aside. So for this reason (and a few others) Stevan
241has re-dubbed Moose a I<postmodern> object system.
242
243Nuff Said.
244
245=back
246
ed5b20d9 247=head1 AUTHORS
270df362 248
ed5b20d9 249Dave Rolsky E<lt>autarch@urth.orgE<gt>
250
251Stevan Little E<lt>stevan@iinteractive.comE<gt>
270df362 252
253=head1 COPYRIGHT AND LICENSE
254
ed5b20d9 255Copyright 2008-2009 by Infinity Interactive, Inc.
270df362 256
257L<http://www.iinteractive.com>
258
259This library is free software; you can redistribute it and/or modify
260it under the same terms as Perl itself.
261
262=cut