Commit | Line | Data |
686a7f09 |
1 | |
2 | =pod |
3 | |
4 | =head1 NAME |
5 | |
6 | Moose::Cookbook::Snack::Perl5ObjsVsMooseObjs - Short comparison between Perl 5 |
7 | objects 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 | |
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... |
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 | |
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? |
81 | |
82 | =head3 Difference #1 - declaration of object attributes |
83 | |
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 |
89 | object. |
90 | |
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. |
98 | |
99 | =head3 Difference #2 - strict and warning pragmas |
100 | |
101 | Moose sets the 'strict' and 'warnings' pragmas for you automatically. We have |
102 | to do this for ourselves in the Perl 5 example. |
103 | |
104 | =head3 Difference #3 - Determining an object's class name |
105 | |
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 |
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 | |
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; |
120 | |
121 | package Perl5Object; |
122 | sub set_x { # some code here } |
123 | package main; |
124 | # later on... |
125 | $self->set_x(0); |
126 | |
127 | or you can assign the value directly to the Perl 5 object attribute like this: |
128 | |
129 | $self->{x} = 0; |
130 | |
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: |
135 | |
136 | package MooseObject; |
137 | has 'x' => (is => 'rw'); |
138 | package main; |
139 | # later on... |
140 | $self->x(0); |
141 | |
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: |
146 | |
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. |
149 | |
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? |
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 | |
167 | Brian Manning <elspicyjack at gmail dot com> |
168 | |
169 | =head1 COPYRIGHT AND LICENSE |
170 | |
171 | Copyright (c)2008 by Brian Manning |
172 | |
173 | This documentation is free software; you can redistribute it and/or modify |
174 | it under the same terms as Perl itself. |
175 | |
176 | =cut |