6 Moose::Cookbook::Snack::Perl5ObjsVsMooseObjs - Short comparison between Perl 5
7 objects and Moose objects
12 use Moose; # automagically sets 'strict' and 'warnings'
14 has q(script_name) => ( is => q(rw), required => 1);
17 use Moose; # needed for the call to 'blessed' below
19 # '$0' is the name of this script, set automatically by Perl
20 my $demo = Moose::Demo->new( script_name => $0 );
22 print qq(My name is ) . $demo->script_name . qq(\n);
23 print qq(I am a ) . blessed $demo . qq( type of object\n);
27 So what's the big stink about Moose? Perl 5 comes with objects and object
28 oriented programming already. Given the above Moose code, what would similar
29 code look like in the existing Perl 5 object-oriented style of programming?
30 Let's take a look and find out...
32 =head2 Perl 5 OO Example
34 # Perl 5 Object, as taught by the 'perltoot' POD page
42 # assign the rest of the method arguments to a temp hash
45 # create the object out of a blessed hash reference
46 my $self = bless ( {}, ref($class) || $class );
47 # create the script_name attribute
48 $self->{script_name} = undef;
50 # verify that the user passed in the 'script_name' attribute
51 if ( exists $args{script_name} ) {
52 $self->script_name($args{script_name});
54 die q(ERROR: can't create object without 'script_name' );
55 } # if ( exists $args{script_name} )
57 # return the object reference back to the caller
63 # check for arguments; use the argument if passed in, otherwise
64 # return the existing value (if any)
65 if (@_) { $self->{script_name} = shift }
66 return $self->{script_name};
73 my $demo = Perl5::Demo->new( script_name => $0 );
75 print qq(My name is ) . $demo->script_name . qq(\n);
76 print qq(I am a ) . ref($demo) . qq( type of object\n);
78 Looks more complex, right? Moose does a lot of the labor when working with
79 Perl objects, so that you don't have to. What are some of the specific
80 differences between Moose and Perl 5 Objects?
82 =head3 Difference #1 - declaration of object attributes
84 Both the Moose and Perl 5 objects have one attribute, C<script_name>. It's a
85 good programming practice to always validate user input, so we have the Perl 5
86 object check to make sure that the user passes in the C<script_name> attribute
87 to it when the object is created. The Moose object automatically checks this
88 for us when we set C<required =E<gt> 1> in the C<has> function for the Moose
91 In more advanced Moose usage, you can use something called 'type constraints'
92 when creating your Moose objects. Type constraints are used to validate what
93 the user passes in when setting Moose object attributes. If the user passes
94 in a type of data that Moose is not expecting, then the type constraints in
95 Moose (specifically, the L<Moose::Util::TypeConstraint> module) will let the
96 user know this in no uncertain terms. Type constraints in Moose can be as
97 simple as strings or numbers, or as complex as other Moose objects.
99 =head3 Difference #2 - strict and warning pragmas
101 Moose sets the 'strict' and 'warnings' pragmas for you automatically. We have
102 to do this for ourselves in the Perl 5 example.
104 =head3 Difference #3 - Determining an object's class name
106 The C<ref()> function in Perl 5 is how you determine an object's class name.
107 The proper way to do this with Moose is C<$object-E<gt>meta-E<gt>name>
108 B<FIXME> $obj->meta->name
110 # an object's class name in Perl 5 OO
111 print qq(I am a ) . ref($demo) . qq( type of object\n);
113 # an object's class name in Moose
114 print qq(I am a ) . blessed $demo->meta->name . qq( type of object\n);
116 =head3 Difference #4 - Assigning values to Moose object attributes
118 When you wish to assign a value directly to an object attribute for a Perl 5
119 object, you can either create an object method that handles the value for you;
122 sub set_x { # some code here }
127 or you can assign the value directly to the Perl 5 object attribute like this:
131 Moose creates object methods for handling attributes for you, as long as you
132 specified C<is =E<gt> rw> for each C<has> statement inside the object
133 declaration. This is mentioned in L<Moose::Cookbook::WTF>, in the section
134 labeld B<Accessors>, but briefly:
137 has 'x' => (is => 'rw');
142 The syntax shown for the Perl 5 object (C<$self-E<gt>{x} = 0>) will also work
143 on the Moose object, as Moose objects are blessed hashes just like the average
144 Perl object is. However, if you access the object's hash reference directly
145 via the latter syntax:
147 1) Moose will no longer be to enforce having that attribute be read-only if
148 you used (C<is =E<gt> ro>) in the object's declaration.
150 2) You break that object's encapsulation, which is one of the reasons you want
151 to use objects in the first place, right?
157 =item L<Moose::Cookbook::Recipe1> - The 'Point' object example
159 =item L<Moose::Util::TypeConstraints> - Type constraints that Moose can use
161 =item L<Moose::Cookbook::WTF> - For when things go wrong with Moose
167 Brian Manning <elspicyjack at gmail dot com>
169 =head1 COPYRIGHT AND LICENSE
171 Copyright (c)2008 by Brian Manning
173 This documentation is free software; you can redistribute it and/or modify
174 it under the same terms as Perl itself.