Commit | Line | Data |
b8790e44 |
1 | =pod |
2 | |
3 | =head1 NAME |
4 | |
5 | Moose::Unsweetened - Moose idioms in plain old Perl 5 without the sugar |
6 | |
7 | =head1 DESCRIPTION |
8 | |
9 | If you're trying to figure out just what the heck Moose does, and how |
10 | it saves you time, you might find it helpful to see what Moose is |
11 | I<really> doing for you. This document shows you the translation from |
12 | Moose sugar back to plain old Perl 5. |
13 | |
14 | =head1 CLASSES AND ATTRIBUTES |
15 | |
16 | First, we define two very small classes the Moose way. |
17 | |
18 | package Person; |
19 | |
20 | use DateTime; |
21 | use DateTime::Format::Natural; |
22 | use Moose; |
23 | use Moose::Util::TypeConstraints; |
24 | |
25 | has name => ( |
26 | is => 'rw', |
27 | isa => 'Str', |
28 | required => 1, |
29 | ); |
30 | |
31 | # Moose doesn't know about non-Moose-based classes. |
32 | class_type 'DateTime'; |
33 | |
34 | my $en_parser = DateTime::Format::Natural->new( |
35 | lang => 'en', |
36 | time_zone => 'UTC', |
37 | ); |
38 | |
39 | coerce 'DateTime' |
40 | => from 'Str' |
41 | => via { $en_parser->parse_datetime($_) }; |
42 | |
43 | has birth_date => ( |
b1ff769c |
44 | is => 'rw', |
45 | isa => 'DateTime', |
46 | coerce => 1, |
47 | handles => { birth_year => 'year' }, |
b8790e44 |
48 | ); |
49 | |
50 | subtype 'ShirtSize' |
51 | => as 'Str' |
52 | => where { /^(?:s|m|l|xl|xxl)$/i } |
53 | => message { "$_ is not a valid shirt size (s, m, l, xl, xxl)" }; |
54 | |
55 | has shirt_size => ( |
56 | is => 'rw', |
57 | isa => 'ShirtSize', |
58 | default => 'l', |
59 | ); |
60 | |
61 | This is a fairly simple class with three attributes. We also define a |
62 | type to validate t-shirt sizes because we don't want to end up with |
63 | something like "blue" for the shirt size! |
64 | |
65 | package User; |
66 | |
67 | use Email::Valid; |
68 | use Moose; |
69 | use Moose::Util::TypeConstraints; |
70 | |
71 | extends 'Person'; |
72 | |
73 | subtype 'Email' |
74 | => as 'Str' |
75 | => where { Email::Valid->address($_) } |
76 | => message { "$_ is not a valid email address" }; |
77 | |
78 | has email_address => ( |
79 | is => 'rw', |
80 | isa => 'Email', |
81 | required => 1, |
82 | ); |
83 | |
84 | This class subclasses Person to add a single attribute, email address. |
85 | |
86 | Now we will show what these classes would look like in plain old Perl |
87 | 5. For the sake of argument, we won't use any base classes or any |
88 | helpers like C<Class::Accessor>. |
89 | |
90 | package Person; |
91 | |
92 | use strict; |
93 | use warnings; |
94 | |
95 | use Carp qw( confess ); |
96 | use DateTime; |
97 | use DateTime::Format::Natural; |
98 | |
99 | |
100 | sub new { |
101 | my $class = shift; |
102 | my %p = ref $_[0] ? %{ $_[0] } : @_; |
103 | |
104 | exists $p{name} |
105 | or confess 'name is a required attribute'; |
106 | $class->_validate_name( $p{name} ); |
107 | |
108 | exists $p{birth_date} |
109 | or confess 'birth_date is a required attribute'; |
110 | |
1900b4d1 |
111 | $p{birth_date} = $class->_coerce_birth_date( $p{birth_date} ); |
112 | $class->_validate_birth_date( $p{birth_date} ); |
b8790e44 |
113 | |
114 | $p{shirt_size} = 'l' |
115 | unless exists $p{shirt_size}: |
116 | |
117 | $class->_validate_shirt_size( $p{shirt_size} ); |
118 | |
1900b4d1 |
119 | return bless \%p, $class; |
b8790e44 |
120 | } |
121 | |
122 | sub _validate_name { |
123 | shift; |
124 | my $name = shift; |
125 | |
126 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; |
127 | |
128 | defined $name |
129 | or confess 'name must be a string'; |
130 | } |
131 | |
132 | { |
133 | my $en_parser = DateTime::Format::Natural->new( |
134 | lang => 'en', |
135 | time_zone => 'UTC', |
136 | ); |
137 | |
138 | sub _coerce_birth_date { |
139 | shift; |
140 | my $date = shift; |
141 | |
50c221c5 |
142 | return $date unless defined $date && ! ref $date; |
b8790e44 |
143 | |
144 | my $dt = $en_parser->parse_datetime($date); |
145 | |
146 | return $dt ? $dt : undef; |
147 | } |
148 | } |
149 | |
150 | sub _validate_birth_date { |
151 | shift; |
152 | my $birth_date = shift; |
153 | |
154 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; |
155 | |
156 | $birth_date->isa('DateTime') ) |
157 | or confess 'birth_date must be a DateTime object'; |
158 | } |
159 | |
160 | sub _validate_shirt_size { |
161 | shift; |
162 | my $shirt_size = shift; |
163 | |
164 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; |
165 | |
166 | defined $shirt_size |
167 | or confess 'shirt_size cannot be undef'; |
168 | |
169 | $shirt_size =~ /^(?:s|m|l|xl|xxl)$/ |
170 | or confess "$shirt_size is not a valid shirt size (s, m, l, xl, xxl)"; |
171 | } |
172 | |
173 | sub name { |
174 | my $self = shift; |
175 | |
176 | if (@_) { |
177 | $self->_validate_name( $_[0] ); |
178 | $self->{name} = $_[0]; |
179 | } |
180 | |
181 | return $self->{name}; |
182 | } |
183 | |
184 | sub birth_date { |
185 | my $self = shift; |
186 | |
187 | if (@_) { |
50c221c5 |
188 | my $date = $self->_coerce_birth_date( $_[0] ); |
b8790e44 |
189 | $self->_validate_birth_date( $date ); |
50c221c5 |
190 | |
b8790e44 |
191 | $self->{birth_date} = $date; |
192 | } |
193 | |
194 | return $self->{birth_date}; |
195 | } |
196 | |
b1ff769c |
197 | sub birth_year { |
198 | my $self = shift; |
199 | |
200 | return $self->birth_date->year; |
201 | } |
202 | |
b8790e44 |
203 | sub shirt_size { |
204 | my $self = shift; |
205 | |
206 | if (@_) { |
207 | $self->_validate_shirt_size( $_[0] ); |
208 | $self->{shirt_size} = $_[0]; |
209 | } |
210 | |
211 | return $self->{shirt_size}; |
212 | } |
213 | |
214 | Wow, that was a mouthful! One thing to note is just how much space the |
215 | data validation code consumes. As a result, it's pretty common for |
216 | Perl 5 programmers to just not bother, which results in much more |
217 | fragile code. |
218 | |
35262b37 |
219 | Did you spot the (intentional) bug? |
b8790e44 |
220 | |
221 | It's in the C<_validate_birth_date()> method. We should check that |
222 | that value in C<$birth_date> is actually defined and object before we |
223 | go and call C<isa()> on it! Leaving out those checks means our data |
224 | validation code could actually cause our program to die. Oops. |
225 | |
b8790e44 |
226 | Also note that if we add a superclass to Person we'll have to change |
227 | the constructor to account for that. |
228 | |
229 | (As an aside, getting all the little details of what Moose does for |
230 | you just right in this code was not easy, which just emphasizes the |
231 | point, that Moose saves you a lot of work!) |
232 | |
233 | Now let's see User: |
234 | |
235 | package User; |
236 | |
237 | use strict; |
238 | use warnings; |
239 | |
240 | use Carp qw( confess ); |
241 | use Email::Valid; |
242 | use Scalar::Util qw( blessed ); |
243 | |
244 | use base 'Person'; |
245 | |
246 | |
247 | sub new { |
248 | my $class = shift; |
249 | my %p = ref $_[0] ? %{ $_[0] } : @_; |
250 | |
251 | exists $p{email_address} |
252 | or confess 'email_address is a required attribute'; |
253 | $class->_validate_email_address( $p{email_address} ); |
254 | |
255 | my $self = $class->SUPER::new(%p); |
256 | |
257 | $self->{email_address} = $p{email_address}; |
258 | |
259 | return $self; |
260 | } |
261 | |
262 | sub _validate_email_address { |
263 | shift; |
264 | my $email_address = shift; |
265 | |
266 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; |
267 | |
268 | defined $email_address |
269 | or confess 'email_address must be a string'; |
270 | |
271 | Email::Valid->address($email_address) |
272 | or confess "$email_address is not a valid email address"; |
273 | } |
274 | |
275 | sub email_address { |
276 | my $self = shift; |
277 | |
278 | if (@_) { |
279 | $self->_validate_email_address( $_[0] ); |
280 | $self->{email_address} = $_[0]; |
281 | } |
282 | |
283 | return $self->{email_address}; |
284 | } |
285 | |
286 | That one was shorter, but it only has one attribute. |
287 | |
288 | Between the two classes, we have a whole lot of code that doesn't do |
289 | much. We could probably simplify this by defining some sort of |
290 | "attribute and validation" hash, like this: |
291 | |
292 | package Person; |
293 | |
294 | my %Attr = ( |
295 | name => { |
296 | required => 1, |
297 | validate => sub { defined $_ }, |
298 | }, |
299 | birth_date => { |
300 | required => 1, |
301 | validate => sub { blessed $_ && $_->isa('DateTime') }, |
302 | }, |
303 | shirt_size => { |
304 | required => 1, |
305 | validate => sub { defined $_ && $_ =~ /^(?:s|m|l|xl|xxl)$/i }, |
306 | } |
307 | ); |
308 | |
309 | Then we could define a base class that would accept such a definition, |
e3494ea7 |
310 | and do the right thing. Keep that sort of thing up and we're well on |
311 | our way to writing a half-assed version of Moose! |
b8790e44 |
312 | |
313 | Of course, there are CPAN modules that do some of what Moose does, |
314 | like C<Class::Accessor>, C<Class::Meta>, and so on. But none of them |
315 | put together all of Moose's features along with a layer of declarative |
b650ff44 |
316 | sugar, nor are these other modules designed for extensibility in the |
317 | same way as Moose. With Moose, it's easy to write a MooseX module to |
318 | replace or extend a piece of built-in functionality. |
b8790e44 |
319 | |
320 | =head1 AUTHOR |
321 | |
322 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
323 | |
324 | =head1 COPYRIGHT AND LICENSE |
325 | |
326 | Copyright 2008 by Infinity Interactive, Inc. |
327 | |
328 | L<http://www.iinteractive.com> |
329 | |
330 | This library is free software; you can redistribute it and/or modify |
331 | it under the same terms as Perl itself. |
332 | |
333 | =cut |