Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 315 lines (214 sloc) 9.294 kb
a7b7353 chromatic Renumbered =head tags for chapters (GH #102).
authored
1 =head1 Blessed References
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
2
7f6a9b5 chromatic Added basic outlines for several new sections.
authored
3 Z<blessed_references>
4
d4dd152 chromatic Edited objects chapter.
authored
5 Perl 5's core object system is deliberately minimal. It has only three rules:
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
6
7 =over 4
8
9 =item * A class is a package.
10
33a6e25 chromatic s/subroutine/function/g
authored
11 =item * A method is a function.
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
12
13 =item * A (blessed) reference is an object.
14
15 =back
16
d4dd152 chromatic Edited objects chapter.
authored
17 You can build anything else out of those three rules, but that's all you get by
18 default. This minimalism can be impractical for larger projects--in particular,
19 the possibilities for greater abstraction through metaprogramming
20 (L<code_generation>) are awkward and limited. Moose (L<moose>) is a better
21 choice for modern programs larger than a couple of hundred lines, although
22 plenty of legacy code still uses Perl 5's default OO.
23
bc04228 chromatic Cleaned up the index.
authored
24 X<OO; C<bless>>
25 X<builtins; C<bless>>
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
26
d4dd152 chromatic Edited objects chapter.
authored
27 The final piece of Perl 5 core OO is the blessed reference. The C<bless>
28 builtin associates the name of a class with a reference. That reference is now
29 a valid invocant, and Perl will perform method dispatch on it, using the
30 associated class.
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
31
bc04228 chromatic Cleaned up the index.
authored
32 X<OO; constructors>
33 X<OO; class methods>
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
34 X<methods; class>
35
d4dd152 chromatic Edited objects chapter.
authored
36 A constructor is a method which creates and blesses a reference. By convention,
37 constructors have the name C<new()>, but this is not a requirement.
38 Constructors are also almost always I<class methods>.
39
40 C<bless> takes two arguments, a reference and a class name. It evaluates to the
41 reference. The reference may be empty. The class does not have to exist yet.
42 You may even use C<bless> outside of a constructor or a class (though all but
43 the simplest programs should use real constructors). The canonical constructor
44 resembles:
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
45
46 =begin programlisting
47
48 sub new
49 {
50 my $class = shift;
51 bless {}, $class;
52 }
53
54 =end programlisting
55
0ed6018 chromatic Edited blessed references section.
authored
56 By design, this constructor receives the class name as the method's invocant.
d4dd152 chromatic Edited objects chapter.
authored
57 You may also hard-code the name of a class, at the expense of flexibility.
58 Parametric constructor allows reuse through inheritance, delegation, or
59 exporting.
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
60
0ed6018 chromatic Edited blessed references section.
authored
61 X<OO; instance data>
62
d4dd152 chromatic Edited objects chapter.
authored
63 The type of reference used is relevant only to how the object stores its own
64 I<instance data>. It has no other effect on the resulting object. Hash
65 references are most common, but you can bless any type of reference:
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
66
67 =begin programlisting
68
69 my $array_obj = bless [], $class;
70 my $scalar_obj = bless \$scalar, $class;
71 my $sub_obj = bless \&some_sub, $class;
72
73 =end programlisting
74
f7af6dd chromatic Applied changes from Alex Scott-Johns.
authored
75 Whereas classes built with Moose define their own object attributes
d4dd152 chromatic Edited objects chapter.
authored
76 declaratively, Perl 5's default OO is lax. A class representing basketball
0ed6018 chromatic Edited blessed references section.
authored
77 players which stores jersey number and position might use a constructor like:
7f6a9b5 chromatic Added basic outlines for several new sections.
authored
78
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
79 =begin programlisting
7f6a9b5 chromatic Added basic outlines for several new sections.
authored
80
d4dd152 chromatic Edited objects chapter.
authored
81 package Player
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
82 {
d4dd152 chromatic Edited objects chapter.
authored
83 sub new
84 {
85 my ($class, %attrs) = @_;
7f6a9b5 chromatic Added basic outlines for several new sections.
authored
86
d4dd152 chromatic Edited objects chapter.
authored
87 bless \%attrs, $class;
88 }
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
89 }
90
91 =end programlisting
92
93 ... and create players with:
94
95 =begin programlisting
96
97 my $joel = Player->new(
98 number => 10,
99 position => 'center',
100 );
101
d4dd152 chromatic Edited objects chapter.
authored
102 my $dante = Player->new(
103 number => 33,
104 position => 'forward',
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
105 );
106
107 =end programlisting
108
d4dd152 chromatic Edited objects chapter.
authored
109 The object's methods can access hash elements directly:
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
110
111 =begin programlisting
112
113 sub format
114 {
115 my $self = shift;
116 return '#' . $self->{number} . ' plays ' . $self->{position};
117 }
118
119 =end programlisting
120
d4dd152 chromatic Edited objects chapter.
authored
121 ... but so can any other code. If external code violates attribute
122 encapsulation, you can never change the object's internal representation
123 without breaking external code. Accessor methods are safer:
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
124
125 =begin programlisting
126
127 sub number { return shift->{number} }
128 sub position { return shift->{position} }
129
130 =end programlisting
131
d4dd152 chromatic Edited objects chapter.
authored
132 ... and now you're starting to write manually what Moose gives you for free.
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
133
134 =begin sidebar
135
d4dd152 chromatic Edited objects chapter.
authored
136 Moose encourages you to write better code by using accessors instead of
137 accessing attributes directly.
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
138
139 =end sidebar
140
a7b7353 chromatic Renumbered =head tags for chapters (GH #102).
authored
141 =head2 Method Lookup and Inheritance
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
142
143 X<method dispatch>
bc04228 chromatic Cleaned up the index.
authored
144 X<OO; methods>
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
145
d4dd152 chromatic Edited objects chapter.
authored
146 Given a blessed reference, a method call of the form:
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
147
148 =begin programlisting
149
150 my $number = $joel->number();
151
152 =end programlisting
153
154 ... looks up the name of the class associated with the blessed reference
d4dd152 chromatic Edited objects chapter.
authored
155 C<$joel>--C<Player>, in this case. Next, Perl looks for a functionN<Remember
156 that Perl 5 makes no distinction between functions in a namespace and methods.>
157 named C<number()> in C<Player>. If no such function exists and if C<Player>
158 extends class, Perl looks in the parent class (and so on and so on) until it
159 finds a C<number()>. If Perl finds C<number()>, it calls that method with
0ed6018 chromatic Edited blessed references section.
authored
160 C<$joel> as an invocant.
e1f73c2 chromatic Wrote most of the blessed references section; still more to come.
authored
161
d4dd152 chromatic Edited objects chapter.
authored
162 =begin sidebar
163
164 X<CPAN; C<namespace::autoclean>>
165
166 The C<namespace::autoclean> CPAN module can help avoid unintentional collisions between imported functions and methods.
167
168 =end sidebar
169
1e71abb chromatic Fixed up indexing.
authored
170 X<C<@ISA>>
bc04228 chromatic Cleaned up the index.
authored
171 X<OO; C<@ISA>>
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
172
d4dd152 chromatic Edited objects chapter.
authored
173 Moose provides C<extends> to track inheritance relationships, but Perl 5 uses a
174 package global variable named C<@ISA>. The method dispatcher looks in each
175 class's C<@ISA> to find the names of its parent classes. If C<InjuredPlayer>
176 extends C<Player>, you might write:
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
177
178 =begin programlisting
179
d4dd152 chromatic Edited objects chapter.
authored
180 package InjuredPlayer
181 {
182 @InjuredPlayer::ISA = 'Player';
183 }
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
184
185 =end programlisting
186
bc04228 chromatic Cleaned up the index.
authored
187 X<C<parent> pragma>
188 X<pragmas; C<parent>>
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
189
d4dd152 chromatic Edited objects chapter.
authored
190 The C<parent> pragma (L<pragmas>) is cleanerN<Older code may use the C<base>
191 pragma, but C<parent> superseded C<base> in Perl 5.10.>:
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
192
193 =begin programlisting
194
d4dd152 chromatic Edited objects chapter.
authored
195 package InjuredPlayer
196 {
197 use parent 'Player';
198 }
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
199
200 =end programlisting
201
d4dd152 chromatic Edited objects chapter.
authored
202 =begin programlisting
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
203
d4dd152 chromatic Edited objects chapter.
authored
204 Moose has its own metamodel which stores extended inheritance information; this
205 offers additional features.
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
206
d4dd152 chromatic Edited objects chapter.
authored
207 =end programlisting
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
208
209 X<multiple inheritance>
bc04228 chromatic Cleaned up the index.
authored
210 X<OO; inheritance>
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
211
212 You may inherit from multiple parent classes:
213
214 =begin programlisting
215
216 package InjuredPlayer;
d4dd152 chromatic Edited objects chapter.
authored
217 {
218 use parent qw( Player Hospital::Patient );
219 }
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
220
221 =end programlisting
222
d4dd152 chromatic Edited objects chapter.
authored
223 ... though the caveats about multiple inheritance and method dispatch
224 complexity apply. Consider instead roles (L<roles>) or Moose method modifiers.
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
225
a7b7353 chromatic Renumbered =head tags for chapters (GH #102).
authored
226 =head2 AUTOLOAD
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
227
1e71abb chromatic Fixed up indexing.
authored
228 X<C<AUTOLOAD>>
bc04228 chromatic Cleaned up the index.
authored
229 X<OO; C<AUTOLOAD>>
1e71abb chromatic Fixed up indexing.
authored
230 X<methods; C<AUTOLOAD>>
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
231
232 If there is no applicable method in the invocant's class or any of its
d4dd152 chromatic Edited objects chapter.
authored
233 superclasses, Perl 5 will next look for an C<AUTOLOAD()> function (L<autoload>)
234 in every class according to the selected method resolution order. Perl will
235 invoke any C<AUTOLOAD()> it finds to provide or decline the desired method.
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
236
d4dd152 chromatic Edited objects chapter.
authored
237 C<AUTOLOAD()> makes multiple inheritance much more difficult to understand.
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
238
a7b7353 chromatic Renumbered =head tags for chapters (GH #102).
authored
239 =head2 Method Overriding and SUPER
0ed6018 chromatic Edited blessed references section.
authored
240
d4dd152 chromatic Edited objects chapter.
authored
241 As with Moose, you may override methods in the core Perl 5 OO. Unlike Moose,
242 core Perl 5 provides no mechanism for indicating your I<intent> to override a
243 parent's method. Worse yet, any function you predeclare, declare, or import
244 into the child class may override a method in the parent class by having the
245 same name. Even if you forget to use the C<override> system of Moose, at least
246 it exists. Core Perl 5 OO offers no such protection.
247
248 X<builtins; C<SUPER::>>
0ed6018 chromatic Edited blessed references section.
authored
249
c1814ed chromatic Applied edits from jethro.
authored
250 To override a method in a child class, declare a method of the same name as the
d4dd152 chromatic Edited objects chapter.
authored
251 method in the parent. Within an overridden method, call the parent method with
c1814ed chromatic Applied edits from jethro.
authored
252 the C<SUPER::> dispatch hint:
0ed6018 chromatic Edited blessed references section.
authored
253
254 =begin programlisting
255
256 sub overridden
257 {
258 my $self = shift;
259 warn "Called overridden() in child!";
260 return $self->SUPER::overridden( @_ );
261 }
262
263 =end programlisting
264
265 The C<SUPER::> prefix to the method name tells the method dispatcher to
d4dd152 chromatic Edited objects chapter.
authored
266 dispatch to an overridden method of the appropriate name. You can provide your
267 own arguments to the overridden method, but most code reuses C<@_>. Be careful
268 to C<shift> off the invocant if you do.
0ed6018 chromatic Edited blessed references section.
authored
269
270 =begin sidebar
271
d4dd152 chromatic Edited objects chapter.
authored
272 X<CPAN; C<SUPER>>
3d8f1e8 chromatic Applied more proofreading edits.
authored
273
d4dd152 chromatic Edited objects chapter.
authored
274 C<SUPER::> has a confusing misfeature: it dispatches to the parent of the
275 package into which the overridden method was I<compiled>. If you've imported
276 this method from another package, Perl will happily dispatch to the I<wrong>
277 parent. The desire for backwards compatibility has kept this misfeature in
278 place. The C<SUPER> module from the CPAN offers a workaround. Moose's
279 C<super()> does not suffer the same problem.
0ed6018 chromatic Edited blessed references section.
authored
280
281 =end sidebar
282
a7b7353 chromatic Renumbered =head tags for chapters (GH #102).
authored
283 =head2 Strategies for Coping with Blessed References
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
284
d4dd152 chromatic Edited objects chapter.
authored
285 If blessed references seem minimal and tricky and confusing, they are. Moose is
286 a tremendous improvement. Use it whenever possible. If you do find yourself
287 maintaining code which uses blessed references, or if you can't convince your
288 team to use Moose in full yet, you can work around some of the problems of
289 blessed references with discipline.
290
291 X<C<Class::Accessor>>
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
292
d4dd152 chromatic Edited objects chapter.
authored
293 =over 4
294
295 =item * Use accessor methods pervasively, even within methods in your class.
296 Consider using a module such as C<Class::Accessor> to avoid repetitive
0ed6018 chromatic Edited blessed references section.
authored
297 boilerplate.
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
298
d4dd152 chromatic Edited objects chapter.
authored
299 =item * Avoid C<AUTOLOAD()> where possible. If you I<must> use it, use forward
300 declarations of your functions (L<functions>) to help Perl know which
301 C<AUTOLOAD()> will provide the method implementation.
302
303 =item * Expect that someone, somewhere will eventually need to subclass (or
304 delegate to or reimplement the interface of) your classes. Make it easier for
305 them by not assuming details of the internals of your code, by using the
306 two-argument form of C<bless>, and by breaking your classes into the smallest
307 responsible units of code.
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
308
d4dd152 chromatic Edited objects chapter.
authored
309 =item * Do not mix functions and methods in the same class.
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
310
d4dd152 chromatic Edited objects chapter.
authored
311 =item * Use a single F<.pm> file for each class, unless the class is a small,
0ed6018 chromatic Edited blessed references section.
authored
312 self-contained helper used from a single place.
fa9101f chromatic Expanded blessed references section; need to address the author notes so...
authored
313
d4dd152 chromatic Edited objects chapter.
authored
314 =back
Something went wrong with that request. Please try again.