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