- initial commit of new snacks (short documents) for ArrayRef's, HashRef's and
[gitmo/Moose.git] / lib / Moose / Cookbook / Snack / Perl5ObjsVsMooseObjs.pod
CommitLineData
686a7f09 1
2=pod
3
4=head1 NAME
5
6Moose::Cookbook::Snack::Perl5ObjsVsMooseObjs - Short comparison between Perl 5
7objects and Moose objects
8
9=head1 SYNOPSIS
10
11 package Moose::Demo;
12 use Moose; # automagically sets 'strict' and 'warnings'
13
14 has q(script_name) => ( is => q(rw), required => 1);
15
16 package main;
17 use Moose; # needed for the call to 'blessed' below
18
19 # '$0' is the name of this script, set automatically by Perl
20 my $demo = Moose::Demo->new( script_name => $0 );
21
22 print qq(My name is ) . $demo->script_name . qq(\n);
23 print qq(I am a ) . blessed $demo . qq( type of object\n);
24
25=head1 DESCRIPTION
26
27So what's the big stink about Moose? Perl 5 comes with objects and object
28oriented programming already. Given the above Moose code, what would similar
29code look like in the existing Perl 5 object-oriented style of programming?
30Let's take a look and find out...
31
32=head2 Perl 5 OO Example
33
34 # Perl 5 Object, as taught by the 'perltoot' POD page
35 package Perl5::Demo;
36 use strict;
37 use warnings;
38
39
40 sub new {
41 my $class = shift;
42 # assign the rest of the method arguments to a temp hash
43 my %args = @_;
44
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;
49
50 # verify that the user passed in the 'script_name' attribute
51 if ( exists $args{script_name} ) {
52 $self->script_name($args{script_name});
53 } else {
54 die q(ERROR: can't create object without 'script_name' );
55 } # if ( exists $args{script_name} )
56
57 # return the object reference back to the caller
58 return $self;
59 } # sub new
60
61 sub script_name {
62 my $self = shift;
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};
67 } # sub script_name
68
69 package main;
70 use strict;
71 use warnings;
72
73 my $demo = Perl5::Demo->new( script_name => $0 );
74
75 print qq(My name is ) . $demo->script_name . qq(\n);
76 print qq(I am a ) . ref($demo) . qq( type of object\n);
77
78Looks more complex, right? Moose does a lot of the labor when working with
79Perl objects, so that you don't have to. What are some of the specific
80differences between Moose and Perl 5 Objects?
81
82=head3 Difference #1 - declaration of object attributes
83
84Both the Moose and Perl 5 objects have one attribute, C<script_name>. It's a
85good programming practice to always validate user input, so we have the Perl 5
86object check to make sure that the user passes in the C<script_name> attribute
87to it when the object is created. The Moose object automatically checks this
88for us when we set C<required =E<gt> 1> in the C<has> function for the Moose
89object.
90
91In more advanced Moose usage, you can use something called 'type constraints'
92when creating your Moose objects. Type constraints are used to validate what
93the user passes in when setting Moose object attributes. If the user passes
94in a type of data that Moose is not expecting, then the type constraints in
95Moose (specifically, the L<Moose::Util::TypeConstraint> module) will let the
96user know this in no uncertain terms. Type constraints in Moose can be as
97simple as strings or numbers, or as complex as other Moose objects.
98
99=head3 Difference #2 - strict and warning pragmas
100
101Moose sets the 'strict' and 'warnings' pragmas for you automatically. We have
102to do this for ourselves in the Perl 5 example.
103
104=head3 Difference #3 - Determining an object's class name
105
106The C<ref()> function in Perl 5 is how you determine an object's class name.
107The proper way to do this with Moose is C<$object-E<gt>meta-E<gt>name>
108B<FIXME> $obj->meta->name
109
110 # an object's class name in Perl 5 OO
111 print qq(I am a ) . ref($demo) . qq( type of object\n);
112
113 # an object's class name in Moose
114 print qq(I am a ) . blessed $demo->meta->name . qq( type of object\n);
115
116=head3 Difference #4 - Assigning values to Moose object attributes
117
118When you wish to assign a value directly to an object attribute for a Perl 5
119object, you can either create an object method that handles the value for you;
120
121 package Perl5Object;
122 sub set_x { # some code here }
123 package main;
124 # later on...
125 $self->set_x(0);
126
127or you can assign the value directly to the Perl 5 object attribute like this:
128
129 $self->{x} = 0;
130
131Moose creates object methods for handling attributes for you, as long as you
132specified C<is =E<gt> rw> for each C<has> statement inside the object
133declaration. This is mentioned in L<Moose::Cookbook::WTF>, in the section
134labeld B<Accessors>, but briefly:
135
136 package MooseObject;
137 has 'x' => (is => 'rw');
138 package main;
139 # later on...
140 $self->x(0);
141
142The syntax shown for the Perl 5 object (C<$self-E<gt>{x} = 0>) will also work
143on the Moose object, as Moose objects are blessed hashes just like the average
144Perl object is. However, if you access the object's hash reference directly
145via the latter syntax:
146
1471) Moose will no longer be to enforce having that attribute be read-only if
148you used (C<is =E<gt> ro>) in the object's declaration.
149
1502) You break that object's encapsulation, which is one of the reasons you want
151to use objects in the first place, right?
152
153=head1 SEE ALSO
154
155=over 4
156
157=item L<Moose::Cookbook::Recipe1> - The 'Point' object example
158
159=item L<Moose::Util::TypeConstraints> - Type constraints that Moose can use
160
161=item L<Moose::Cookbook::WTF> - For when things go wrong with Moose
162
163=back
164
165=head1 AUTHOR
166
167Brian Manning <elspicyjack at gmail dot com>
168
169=head1 COPYRIGHT AND LICENSE
170
171Copyright (c)2008 by Brian Manning
172
173This documentation is free software; you can redistribute it and/or modify
174it under the same terms as Perl itself.
175
176=cut