1 package Moose::Manual::Unsweetened;
3 # ABSTRACT: Moose idioms in plain old Perl 5 without the sugar
11 If you're trying to figure out just what the heck Moose does, and how
12 it saves you time, you might find it helpful to see what Moose is
13 I<really> doing for you. This document shows you the translation from
14 Moose sugar back to plain old Perl 5.
16 =head1 CLASSES AND ATTRIBUTES
18 First, we define two very small classes the Moose way.
23 use DateTime::Format::Natural;
25 use Moose::Util::TypeConstraints;
33 # Moose doesn't know about non-Moose-based classes.
34 class_type 'DateTime';
36 my $en_parser = DateTime::Format::Natural->new(
43 => via { $en_parser->parse_datetime($_) };
49 handles => { birth_year => 'year' },
52 enum 'ShirtSize' => qw( s m l xl xxl );
60 This is a fairly simple class with three attributes. We also define an enum
61 type to validate t-shirt sizes because we don't want to end up with something
62 like "blue" for the shirt size!
68 use Moose::Util::TypeConstraints;
74 => where { Email::Valid->address($_) }
75 => message { "$_ is not a valid email address" };
77 has email_address => (
83 This class subclasses Person to add a single attribute, email address.
85 Now we will show what these classes would look like in plain old Perl
86 5. For the sake of argument, we won't use any base classes or any
87 helpers like C<Class::Accessor>.
94 use Carp qw( confess );
96 use DateTime::Format::Natural;
100 my %p = ref $_[0] ? %{ $_[0] } : @_;
103 or confess 'name is a required attribute';
104 $class->_validate_name( $p{name} );
106 exists $p{birth_date}
107 or confess 'birth_date is a required attribute';
109 $p{birth_date} = $class->_coerce_birth_date( $p{birth_date} );
110 $class->_validate_birth_date( $p{birth_date} );
113 unless exists $p{shirt_size}:
115 $class->_validate_shirt_size( $p{shirt_size} );
117 return bless \%p, $class;
124 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
127 or confess 'name must be a string';
131 my $en_parser = DateTime::Format::Natural->new(
136 sub _coerce_birth_date {
140 return $date unless defined $date && ! ref $date;
142 my $dt = $en_parser->parse_datetime($date);
144 return $dt ? $dt : undef;
148 sub _validate_birth_date {
150 my $birth_date = shift;
152 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
154 $birth_date->isa('DateTime')
155 or confess 'birth_date must be a DateTime object';
158 sub _validate_shirt_size {
160 my $shirt_size = shift;
162 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
165 or confess 'shirt_size cannot be undef';
167 my %sizes = map { $_ => 1 } qw( s m l xl xxl );
170 or confess "$shirt_size is not a valid shirt size (s, m, l, xl, xxl)";
177 $self->_validate_name( $_[0] );
178 $self->{name} = $_[0];
181 return $self->{name};
188 my $date = $self->_coerce_birth_date( $_[0] );
189 $self->_validate_birth_date( $date );
191 $self->{birth_date} = $date;
194 return $self->{birth_date};
200 return $self->birth_date->year;
207 $self->_validate_shirt_size( $_[0] );
208 $self->{shirt_size} = $_[0];
211 return $self->{shirt_size};
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. Unfortunately, not validating
217 arguments leads to surprises down the line ("why is birth_date an
220 Also, did you spot the (intentional) bug?
222 It's in the C<_validate_birth_date()> method. We should check that
223 the value in C<$birth_date> is actually defined and an object before
224 we go and call C<isa()> on it! Leaving out those checks means our data
225 validation code could actually cause our program to die. Oops.
227 Note that if we add a superclass to Person we'll have to change the
228 constructor to account for that.
230 (As an aside, getting all the little details of what Moose does for
231 you just right in this example was really not easy, which emphasizes
232 the point of the example. Moose saves you a lot of work!)
241 use Carp qw( confess );
243 use Scalar::Util qw( blessed );
249 my %p = ref $_[0] ? %{ $_[0] } : @_;
251 exists $p{email_address}
252 or confess 'email_address is a required attribute';
253 $class->_validate_email_address( $p{email_address} );
255 my $self = $class->SUPER::new(%p);
257 $self->{email_address} = $p{email_address};
262 sub _validate_email_address {
264 my $email_address = shift;
266 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
268 defined $email_address
269 or confess 'email_address must be a string';
271 Email::Valid->address($email_address)
272 or confess "$email_address is not a valid email address";
279 $self->_validate_email_address( $_[0] );
280 $self->{email_address} = $_[0];
283 return $self->{email_address};
286 That one was shorter, but it only has one attribute.
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:
297 validate => sub { defined $_ },
301 validate => sub { blessed $_ && $_->isa('DateTime') },
305 validate => sub { defined $_ && $_ =~ /^(?:s|m|l|xl|xxl)$/i },
309 Then we could define a base class that would accept such a definition,
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!
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
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.
320 Moose is a complete OO package in and of itself, and is part of a rich
321 ecosystem of extensions. It also has an enthusiastic community of
322 users, and is being actively maintained and developed.