5 Moose::Unsweetened - Moose idioms in plain old Perl 5 without the sugar
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.
14 =head1 CLASSES AND ATTRIBUTES
16 First, we define two very small classes the Moose way.
21 use DateTime::Format::Natural;
23 use Moose::Util::TypeConstraints;
31 # Moose doesn't know about non-Moose-based classes.
32 class_type 'DateTime';
34 my $en_parser = DateTime::Format::Natural->new(
41 => via { $en_parser->parse_datetime($_) };
47 handles => { birth_year => 'year' },
52 => where { /^(?:s|m|l|xl|xxl)$/i }
53 => message { "$_ is not a valid shirt size (s, m, l, xl, xxl)" };
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!
69 use Moose::Util::TypeConstraints;
75 => where { Email::Valid->address($_) }
76 => message { "$_ is not a valid email address" };
78 has email_address => (
84 This class subclasses Person to add a single attribute, email address.
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>.
95 use Carp qw( confess );
97 use DateTime::Format::Natural;
102 my %p = ref $_[0] ? %{ $_[0] } : @_;
105 or confess 'name is a required attribute';
106 $class->_validate_name( $p{name} );
108 exists $p{birth_date}
109 or confess 'birth_date is a required attribute';
111 $p{birth_date} = $class->_coerce_birth_date($date );
112 $class->_validate_birth_date( $date );
115 unless exists $p{shirt_size}:
117 $class->_validate_shirt_size( $p{shirt_size} );
119 my %self = map { $_ => $p{$_} } qw( name shirt_size );
120 $self{birth_date} = $date;
122 return bless \%self, $class;
129 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
132 or confess 'name must be a string';
136 my $en_parser = DateTime::Format::Natural->new(
141 sub _coerce_birth_date {
145 return $date unless defined $date && ! ref $date;
147 my $dt = $en_parser->parse_datetime($date);
149 return $dt ? $dt : undef;
153 sub _validate_birth_date {
155 my $birth_date = shift;
157 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
159 $birth_date->isa('DateTime') )
160 or confess 'birth_date must be a DateTime object';
163 sub _validate_shirt_size {
165 my $shirt_size = shift;
167 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
170 or confess 'shirt_size cannot be undef';
172 $shirt_size =~ /^(?:s|m|l|xl|xxl)$/
173 or confess "$shirt_size is not a valid shirt size (s, m, l, xl, xxl)";
180 $self->_validate_name( $_[0] );
181 $self->{name} = $_[0];
184 return $self->{name};
191 my $date = $self->_coerce_birth_date( $_[0] );
192 $self->_validate_birth_date( $date );
194 $self->{birth_date} = $date;
197 return $self->{birth_date};
203 return $self->birth_date->year;
210 $self->_validate_shirt_size( $_[0] );
211 $self->{shirt_size} = $_[0];
214 return $self->{shirt_size};
217 Wow, that was a mouthful! One thing to note is just how much space the
218 data validation code consumes. As a result, it's pretty common for
219 Perl 5 programmers to just not bother, which results in much more
222 Did you spot the (intentional) bug?
224 It's in the C<_validate_birth_date()> method. We should check that
225 that value in C<$birth_date> is actually defined and object before we
226 go and call C<isa()> on it! Leaving out those checks means our data
227 validation code could actually cause our program to die. Oops.
229 There's one bit of code in there worth explaining, which is the
230 handling of the birth date for coercion. In both the constructor and
231 accessor, we first take a copy of the birth date before passing it to
232 the coercion routine. This is to avoid changing the value as it was
233 passed to those methods, which could cause problems for the caller.
235 Also note that if we add a superclass to Person we'll have to change
236 the constructor to account for that.
238 (As an aside, getting all the little details of what Moose does for
239 you just right in this code was not easy, which just emphasizes the
240 point, that Moose saves you a lot of work!)
249 use Carp qw( confess );
251 use Scalar::Util qw( blessed );
258 my %p = ref $_[0] ? %{ $_[0] } : @_;
260 exists $p{email_address}
261 or confess 'email_address is a required attribute';
262 $class->_validate_email_address( $p{email_address} );
264 my $self = $class->SUPER::new(%p);
266 $self->{email_address} = $p{email_address};
271 sub _validate_email_address {
273 my $email_address = shift;
275 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
277 defined $email_address
278 or confess 'email_address must be a string';
280 Email::Valid->address($email_address)
281 or confess "$email_address is not a valid email address";
288 $self->_validate_email_address( $_[0] );
289 $self->{email_address} = $_[0];
292 return $self->{email_address};
295 That one was shorter, but it only has one attribute.
297 Between the two classes, we have a whole lot of code that doesn't do
298 much. We could probably simplify this by defining some sort of
299 "attribute and validation" hash, like this:
306 validate => sub { defined $_ },
310 validate => sub { blessed $_ && $_->isa('DateTime') },
314 validate => sub { defined $_ && $_ =~ /^(?:s|m|l|xl|xxl)$/i },
318 Then we could define a base class that would accept such a definition,
319 and do the right thing. Keep that sort of thing up and we're well on
320 our way to writing a half-assed version of Moose!
322 Of course, there are CPAN modules that do some of what Moose does,
323 like C<Class::Accessor>, C<Class::Meta>, and so on. But none of them
324 put together all of Moose's features along with a layer of declarative
325 sugar, nor are these other modules designed for extensibility in the
326 same way as Moose. With Moose, it's easy to write a MooseX module to
327 replace or extend a piece of built-in functionality.
331 Dave Rolsky E<lt>autarch@urth.orgE<gt>
333 =head1 COPYRIGHT AND LICENSE
335 Copyright 2008 by Infinity Interactive, Inc.
337 L<http://www.iinteractive.com>
339 This library is free software; you can redistribute it and/or modify
340 it under the same terms as Perl itself.