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 version 2.2012
10
12 Moose is a complete object system for Perl 5. Consider any modern
13 object-oriented language (which Perl 5 definitely isn't). It provides
14 keywords for attribute declaration, object construction, inheritance,
15 and maybe more. These keywords are part of the language, and you don't
16 care how they are implemented.
17
18 Moose aims to do the same thing for Perl 5 OO. We can't actually create
19 new keywords, but we do offer "sugar" that looks a lot like them. More
20 importantly, with Moose, you define your class declaratively, without
21 needing to know about blessed hashrefs, accessor methods, and so on.
22
23 With Moose, you can concentrate on the logical structure of your
24 classes, focusing on "what" rather than "how". A class definition with
25 Moose reads like a list of very concise English sentences.
26
27 Moose is built on top of "Class::MOP", a meta-object protocol (aka
28 MOP). Using the MOP, Moose provides complete introspection for all
29 Moose-using classes. This means you can ask classes about their
30 attributes, parents, children, methods, etc., all using a well-defined
31 API. The MOP abstracts away the symbol table, looking at @ISA vars, and
32 all the other crufty Perl tricks we know and love(?).
33
34 Moose is based in large part on the Perl 6 object system, as well as
35 drawing on the best ideas from CLOS, Smalltalk, and many other
36 languages.
37
39 Moose makes Perl 5 OO both simpler and more powerful. It encapsulates
40 Perl 5 power tools in high-level declarative APIs which are easy to
41 use. Best of all, you don't need to be a wizard to use it.
42
43 But if you want to dig about in the guts, Moose lets you do that too,
44 by using and extending its powerful introspection API.
45
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
61 no Moose;
62 __PACKAGE__->meta->make_immutable;
63
64 This is a 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',
75 isa => 'Str',
76 );
77
78 has 'last_login' => (
79 is => 'rw',
80 isa => 'DateTime',
81 handles => { 'date_of_last_login' => 'date' },
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
95 no Moose;
96 __PACKAGE__->meta->make_immutable;
97
98 When ready to instantiate your class in an application, use it in the
99 "traditional" Perl manner:
100
101 use User;
102
103 my $user = User->new(
104 first_name => 'Example',
105 last_name => 'User',
106 password => 'letmein',
107 );
108
109 $user->login('letmein');
110
111 say $user->date_of_last_login;
112
113 We'll leave the line-by-line explanation of this code to other
114 documentation, but you can see how Moose reduces common OO idioms to
115 simple declarative constructs.
116
118 This manual consists of a number of documents.
119
120 Moose::Manual::Concepts
121 Introduces Moose concepts, and contrasts them against "old school"
122 Perl 5 OO.
123
124 Moose::Manual::Unsweetened
125 Shows two example classes, each written first with Moose and then
126 with "plain old Perl 5".
127
128 Moose::Manual::Classes
129 How do you make use of Moose in your classes? Now that I'm a Moose,
130 how do I subclass something?
131
132 Moose::Manual::Attributes
133 Attributes are a core part of the Moose OO system. An attribute is
134 a piece of data that an object has. Moose has a lot of attribute-
135 related features!
136
137 Moose::Manual::Delegation
138 Delegation is a powerful way to make use of attributes which are
139 themselves objects.
140
141 Moose::Manual::Construction
142 Learn how objects are built in Moose, and in particular about the
143 "BUILD" and "BUILDARGS" methods. Also covers object destruction
144 with "DEMOLISH".
145
146 Moose::Manual::MethodModifiers
147 A method modifier lets you say "before calling method X, do this
148 first", or "wrap method X in this code". Method modifiers are
149 particularly handy in roles and with attribute accessors.
150
151 Moose::Manual::Roles
152 A role is something a class does (like "Debuggable" or
153 "Printable"). Roles provide a way of adding behavior to classes
154 that is orthogonal to inheritance.
155
156 Moose::Manual::Types
157 Moose's type system lets you strictly define what values an
158 attribute can contain.
159
160 Moose::Manual::MOP
161 Moose's meta API system lets you ask classes about their parents,
162 children, methods, attributes, etc.
163
164 Moose::Manual::MooseX
165 This document describes a few of the most useful Moose extensions
166 on CPAN.
167
168 Moose::Manual::BestPractices
169 Moose has a lot of features, and there's definitely more than one
170 way to do it. However, we think that picking a subset of these
171 features and using them consistently makes everyone's life easier.
172
173 Moose::Manual::FAQ
174 Frequently asked questions about Moose.
175
176 Moose::Manual::Resources
177 Links to various tutorials, videos, blogs, presentations,
178 interviews, etc...
179
180 Moose::Manual::Contributing
181 Interested in hacking on Moose? Read this.
182
183 Moose::Manual::Delta
184 This document details backwards-incompatibilities and other major
185 changes to Moose.
186
188 If you're still asking yourself "Why do I need this?", then this
189 section is for you.
190
191 Another object system!?!?
192 Yes, we know there are many, many ways to build objects in Perl 5,
193 many of them based on inside-out objects and other such things.
194 Moose is different because it is not a new object system for Perl
195 5, but instead an extension of the existing object system.
196
197 Moose is built on top of Class::MOP, which is a metaclass system
198 for Perl 5. This means that Moose not only makes building normal
199 Perl 5 objects better, but it also provides the power of metaclass
200 programming.
201
202 Is this for real? Or is this just an experiment?
203 Moose is based on the prototypes and experiments Stevan did for the
204 Perl 6 meta-model. However, Moose is NOT an experiment or
205 prototype; it is for real.
206
207 Is this ready for use in production?
208 Yes.
209
210 Moose has been used successfully in production environments by many
211 people and companies. There are Moose applications which have been
212 in production with little or no issue now for years. We consider it
213 highly stable and we are committed to keeping it stable.
214
215 Of course, in the end, you need to make this call yourself. If you
216 have any questions or concerns, please feel free to email Stevan or
217 the moose@perl.org list, or just stop by irc.perl.org#moose and ask
218 away.
219
220 Is Moose just Perl 6 in Perl 5?
221 No. While Moose is very much inspired by Perl 6, it is not itself
222 Perl 6. Instead, it is an OO system for Perl 5. Stevan built Moose
223 because he was tired of writing the same old boring Perl 5 OO code,
224 and drooling over Perl 6 OO. So instead of switching to Ruby, he
225 wrote Moose :)
226
227 Wait, post modern, I thought it was just modern?
228 Stevan read Larry Wall's talk from the 1999 Linux World entitled
229 "Perl, the first postmodern computer language" in which he talks
230 about how he picked the features for Perl because he thought they
231 were cool and he threw out the ones that he thought sucked. This
232 got him thinking about how we have done the same thing in Moose.
233 For Moose, we have "borrowed" features from Perl 6, CLOS (LISP),
234 Smalltalk, Java, BETA, OCaml, Ruby and more, and the bits we didn't
235 like (cause they sucked) we tossed aside. So for this reason (and a
236 few others) Stevan has re-dubbed Moose a postmodern object system.
237
238 Nuff Said.
239
241 · Stevan Little <stevan.little@iinteractive.com>
242
243 · Dave Rolsky <autarch@urth.org>
244
245 · Jesse Luehrs <doy@tozt.net>
246
247 · Shawn M Moore <code@sartak.org>
248
249 · יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
250
251 · Karen Etheridge <ether@cpan.org>
252
253 · Florian Ragwitz <rafl@debian.org>
254
255 · Hans Dieter Pearcey <hdp@weftsoar.net>
256
257 · Chris Prather <chris@prather.org>
258
259 · Matt S Trout <mst@shadowcat.co.uk>
260
262 This software is copyright (c) 2006 by Infinity Interactive, Inc.
263
264 This is free software; you can redistribute it and/or modify it under
265 the same terms as the Perl 5 programming language system itself.
266
267
268
269perl v5.30.1 2020-01-30 Moose::Manual(3)