5 Moose::Manual::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' },
50 enum 'ShirtSize' => qw( s m l xl xxl );
58 This is a fairly simple class with three attributes. We also define an enum
59 type to validate t-shirt sizes because we don't want to end up with something
60 like "blue" for the shirt size!
66 use Moose::Util::TypeConstraints;
72 => where { Email::Valid->address($_) }
73 => message { "$_ is not a valid email address" };
75 has email_address => (
81 This class subclasses Person to add a single attribute, email address.
83 Now we will show what these classes would look like in plain old Perl
84 5. For the sake of argument, we won't use any base classes or any
85 helpers like C<Class::Accessor>.
92 use Carp qw( confess );
94 use DateTime::Format::Natural;
98 my %p = ref $_[0] ? %{ $_[0] } : @_;
101 or confess 'name is a required attribute';
102 $class->_validate_name( $p{name} );
104 exists $p{birth_date}
105 or confess 'birth_date is a required attribute';
107 $p{birth_date} = $class->_coerce_birth_date( $p{birth_date} );
108 $class->_validate_birth_date( $p{birth_date} );
111 unless exists $p{shirt_size}:
113 $class->_validate_shirt_size( $p{shirt_size} );
115 return bless \%p, $class;
122 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
125 or confess 'name must be a string';
129 my $en_parser = DateTime::Format::Natural->new(
134 sub _coerce_birth_date {
138 return $date unless defined $date && ! ref $date;
140 my $dt = $en_parser->parse_datetime($date);
142 return $dt ? $dt : undef;
146 sub _validate_birth_date {
148 my $birth_date = shift;
150 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
152 $birth_date->isa('DateTime')
153 or confess 'birth_date must be a DateTime object';
156 sub _validate_shirt_size {
158 my $shirt_size = shift;
160 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
163 or confess 'shirt_size cannot be undef';
165 my %sizes = map { $_ => 1 } qw( s m l xl xxl );
168 or confess "$shirt_size is not a valid shirt size (s, m, l, xl, xxl)";
175 $self->_validate_name( $_[0] );
176 $self->{name} = $_[0];
179 return $self->{name};
186 my $date = $self->_coerce_birth_date( $_[0] );
187 $self->_validate_birth_date( $date );
189 $self->{birth_date} = $date;
192 return $self->{birth_date};
198 return $self->birth_date->year;
205 $self->_validate_shirt_size( $_[0] );
206 $self->{shirt_size} = $_[0];
209 return $self->{shirt_size};
212 Wow, that was a mouthful! One thing to note is just how much space the
213 data validation code consumes. As a result, it's pretty common for
214 Perl 5 programmers to just not bother. Unfortunately, not validating
215 arguments leads to surprises down the line ("why is birth_date an
218 Also, did you spot the (intentional) bug?
220 It's in the C<_validate_birth_date()> method. We should check that
221 the value in C<$birth_date> is actually defined and an object before
222 we go and call C<isa()> on it! Leaving out those checks means our data
223 validation code could actually cause our program to die. Oops.
225 Note that if we add a superclass to Person we'll have to change the
226 constructor to account for that.
228 (As an aside, getting all the little details of what Moose does for
229 you just right in this example was really not easy, which emphasizes
230 the point of the example. Moose saves you a lot of work!)
239 use Carp qw( confess );
241 use Scalar::Util qw( blessed );
247 my %p = ref $_[0] ? %{ $_[0] } : @_;
249 exists $p{email_address}
250 or confess 'email_address is a required attribute';
251 $class->_validate_email_address( $p{email_address} );
253 my $self = $class->SUPER::new(%p);
255 $self->{email_address} = $p{email_address};
260 sub _validate_email_address {
262 my $email_address = shift;
264 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
266 defined $email_address
267 or confess 'email_address must be a string';
269 Email::Valid->address($email_address)
270 or confess "$email_address is not a valid email address";
277 $self->_validate_email_address( $_[0] );
278 $self->{email_address} = $_[0];
281 return $self->{email_address};
284 That one was shorter, but it only has one attribute.
286 Between the two classes, we have a whole lot of code that doesn't do
287 much. We could probably simplify this by defining some sort of
288 "attribute and validation" hash, like this:
295 validate => sub { defined $_ },
299 validate => sub { blessed $_ && $_->isa('DateTime') },
303 validate => sub { defined $_ && $_ =~ /^(?:s|m|l|xl|xxl)$/i },
307 Then we could define a base class that would accept such a definition,
308 and do the right thing. Keep that sort of thing up and we're well on
309 our way to writing a half-assed version of Moose!
311 Of course, there are CPAN modules that do some of what Moose does,
312 like C<Class::Accessor>, C<Class::Meta>, and so on. But none of them
313 put together all of Moose's features along with a layer of declarative
314 sugar, nor are these other modules designed for extensibility in the
315 same way as Moose. With Moose, it's easy to write a MooseX module to
316 replace or extend a piece of built-in functionality.
318 Moose is a complete OO package in and of itself, and is part of a rich
319 ecosystem of extensions. It also has an enthusiastic community of
320 users, and is being actively maintained and developed.
324 Dave Rolsky E<lt>autarch@urth.orgE<gt>
326 =head1 COPYRIGHT AND LICENSE
328 Copyright 2009 by Infinity Interactive, Inc.
330 L<http://www.iinteractive.com>
332 This library is free software; you can redistribute it and/or modify
333 it under the same terms as Perl itself.