Penguin
Annotated edit history of perlobj(1) version 2, including all changes. View license author blame.
Rev Author # Line
1 perry 1 PERLOBJ
2 !!!PERLOBJ
3 NAME
4 DESCRIPTION
5 SEE ALSO
6 ----
7 !!NAME
8
9
10 perlobj - Perl objects
11 !!DESCRIPTION
12
13
14 First you need to understand what references are in Perl.
15 See perlref for that. Second, if you still find the
16 following reference work too complicated, a tutorial on
17 object-oriented programming in Perl can be found in perltoot
18 and perltootc.
19
20
21 If you're still with us, then here are three very simple
22 definitions that you should find reassuring.
23
24
25 1.
26
27
28 An object is simply a reference that happens to know which
29 class it belongs to.
30
31
32 2.
33
34
35 A class is simply a package that happens to provide methods
36 to deal with object references.
37
38
39 3.
40
41
42 A method is simply a subroutine that expects an object
43 reference (or a package name, for class methods) as the
44 first argument.
45
46
47 We'll cover these points now in more depth.
48
49
50 __An Object is Simply a Reference__
51
52
53 Unlike say C ++ , Perl doesn't provide any
54 special syntax for constructors. A constructor is merely a
55 subroutine that returns a reference to something ``blessed''
56 into a class, generally the class that the subroutine is
57 defined in. Here is a typical constructor:
58
59
60 package Critter;
61 sub new { bless {} }
62 That word new isn't special. You could have written a construct this way, too:
63
64
65 package Critter;
66 sub spawn { bless {} }
67 This might even be preferable, because the C ++ programmers won't be tricked into thinking that new works in Perl as it does in C ++ . It doesn't. We recommend that you name your constructors whatever makes sense in the context of the problem you're solving. For example, constructors in the Tk extension to Perl are named after the widgets they create.
68
69
70 One thing that's different about Perl constructors compared
71 with those in C ++ is that in Perl, they have
72 to allocate their own memory. (The other things is that they
73 don't automatically call overridden base-class
74 constructors.) The {} allocates an anonymous hash
75 containing no key/value pairs, and returns it The
76 ''bless()'' takes that reference and tells the object it
77 references that it's now a Critter, and returns the
78 reference. This is for convenience, because the referenced
79 object itself knows that it has been blessed, and the
80 reference to it could have been returned directly, like
81 this:
82
83
84 sub new {
85 my $self = {};
86 bless $self;
87 return $self;
88 }
89 You often see such a thing in more complicated constructors that wish to call methods in the class as part of the construction:
90
91
92 sub new {
93 my $self = {};
94 bless $self;
95 $self-
96 If you care about inheritance (and you should; see ``Modules: Creation, Use, and Abuse'' in perlmodlib), then you want to use the two-arg form of bless so that your constructors may be inherited:
97
98
99 sub new {
100 my $class = shift;
101 my $self = {};
102 bless $self, $class;
103 $self-
104 Or if you expect people to call not just CLASS- but also $obj-, then use something like this. The ''initialize()'' method used will be of whatever $class we blessed the object into:
105
106
107 sub new {
108 my $this = shift;
109 my $class = ref($this) $this;
110 my $self = {};
111 bless $self, $class;
112 $self-
113 Within the class package, the methods will typically deal with the reference as an ordinary reference. Outside the class package, the reference is generally treated as an opaque value that may be accessed only through the class's methods.
114
115
116 Although a constructor can in theory re-bless a referenced
117 object currently belonging to another class, this is almost
118 certainly going to get you into trouble. The new class is
119 responsible for all cleanup later. The previous blessing is
120 forgotten, as an object may belong to only one class at a
121 time. (Although of course it's free to inherit methods from
122 many classes.) If you find yourself having to do this, the
123 parent class is probably misbehaving, though.
124
125
126 A clarification: Perl objects are blessed. References are
127 not. Objects know which package they belong to. References
128 do not. The ''bless()'' function uses the reference to
129 find the object. Consider the following
130 example:
131
132
133 $a = {};
134 $b = $a;
135 bless $a, BLAH;
136 print
137 This reports $b as being a BLAH , so obviously ''bless()'' operated on the object and not on the reference.
138
139
140 __A Class is Simply a Package__
141
142
143 Unlike say C ++ , Perl doesn't provide any
144 special syntax for class definitions. You use a package as a
145 class by putting method definitions into the
146 class.
147
148
149 There is a special array within each package called
150 @ISA, which says where else to look for a method if
151 you can't find it in the current package. This is how Perl
152 implements inheritance. Each element of the @ISA
153 array is just the name of another package that happens to be
154 a class package. The classes are searched (depth first) for
155 missing methods in the order that they occur in
156 @ISA. The classes accessible through @ISA
157 are known as base classes of the current class.
158
159
160 All classes implicitly inherit from class UNIVERSAL
161 as their last base class. Several commonly used methods are
162 automatically supplied in the UNIVERSAL
163 class; see ``Default UNIVERSAL methods'' for
164 more details.
165
166
167 If a missing method is found in a base class, it is cached
168 in the current class for efficiency. Changing @ISA
169 or defining new subroutines invalidates the cache and causes
170 Perl to do the lookup again.
171
172
173 If neither the current class, its named base classes, nor
174 the UNIVERSAL class contains the requested
175 method, these three places are searched all over again, this
176 time looking for a method named ''AUTOLOAD
177 ()''. If an AUTOLOAD is found, this method
178 is called on behalf of the missing method, setting the
179 package global $AUTOLOAD to be the fully qualified
180 name of the method that was intended to be
181 called.
182
183
184 If none of that works, Perl finally gives up and
185 complains.
186
187
188 If you want to stop the AUTOLOAD inheritance
189 say simply
190
191
192 sub AUTOLOAD;
193 and the call will die using the name of the sub being called.
194
195
196 Perl classes do method inheritance only. Data inheritance is
197 left up to the class itself. By and large, this is not a
198 problem in Perl, because most classes model the attributes
199 of their object using an anonymous hash, which serves as its
200 own little namespace to be carved up by the various classes
201 that might want to do something with the object. The only
202 problem with this is that you can't sure that you aren't
203 using a piece of the hash that isn't already used. A
204 reasonable workaround is to prepend your fieldname in the
205 hash with the package name.
206
207
208 sub bump {
209 my $self = shift;
210 $self-
211
212
213 __A Method is Simply a Subroutine__
214
215
216 Unlike say C ++ , Perl doesn't provide any
217 special syntax for method definition. (It does provide a
218 little syntax for method invocation though. More on that
219 later.) A method expects its first argument to be the object
220 (reference) or package (string) it is being invoked on.
221 There are two ways of calling methods, which we'll call
222 class methods and instance methods.
223
224
225 A class method expects a class name as the first argument.
226 It provides functionality for the class as a whole, not for
227 any individual object belonging to the class. Constructors
228 are often class methods, but see perltoot and perltootc for
229 alternatives. Many class methods simply ignore their first
230 argument, because they already know what package they're in
231 and don't care what package they were invoked via. (These
232 aren't necessarily the same, because class methods follow
233 the inheritance tree just like ordinary instance methods.)
234 Another typical use for class methods is to look up an
235 object by name:
236
237
238 sub find {
239 my ($class, $name) = @_;
240 $objtable{$name};
241 }
242 An instance method expects an object reference as its first argument. Typically it shifts the first argument into a ``self'' or ``this'' variable, and then uses that as an ordinary reference.
243
244
245 sub display {
246 my $self = shift;
247 my @keys = @_ ? @_ : sort keys %$self;
248 foreach $key (@keys) {
249 print
250
251
252 __Method Invocation__
253
254
255 There are two ways to invoke a method, one of which you're
256 already familiar with, and the other of which will look
257 familiar. Perl 4 already had an ``indirect object'' syntax
258 that you use when you say
259
260
261 print STDERR
262 This same syntax can be used to call either class or instance methods. We'll use the two methods defined above, the class method to lookup an object reference and the instance method to print out its attributes.
263
264
265 $fred = find Critter
266 These could be combined into one statement by using a BLOCK in the indirect object slot:
267
268
269 display {find Critter
270 For C ++ fans, there's also a syntax using -
271
272
273 $fred = Critter-
274 or in one statement,
275
276
277 Critter-
278 There are times when one syntax is more readable, and times when the other syntax is more readable. The indirect object syntax is less cluttered, but it has the same ambiguity as ordinary list operators. Indirect object method calls are usually parsed using the same rule as list operators: ``If it looks like a function, it is a function''. (Presuming for the moment that you think two words in a row can look like a function name. C ++ programmers seem to think so with some regularity, especially when the first word is ``new''.) Thus, the parentheses of
279
280
281 new Critter ('Barney', 1.5, 70)
282 are assumed to surround ALL the arguments of the method call, regardless of what comes after. Saying
283
284
285 new Critter ('Bam' x 2), 1.4, 45
286 would be equivalent to
287
288
289 Critter-
290 which is unlikely to do what you want. Confusingly, however, this rule applies only when the indirect object is a bareword package name, not when it's a scalar, a BLOCK , or a Package:: qualified package name. In those cases, the arguments are parsed in the same way as an indirect object list operator like print, so
291
292
293 new Critter:: ('Bam' x 2), 1.4, 45
294 is the same as
295
296
297 Critter::-
298 For more reasons why the indirect object syntax is ambiguous, see `` WARNING '' below.
299
300
301 There are times when you wish to specify which class's
302 method to use. Here you can call your method as an ordinary
303 subroutine call, being sure to pass the requisite first
304 argument explicitly:
305
306
2 perry 307 $fred = !MyCritter::find(
1 perry 308 Unlike method calls, function calls don't consider inheritance. If you wish merely to specify that Perl should ''START'' looking for a method in a particular package, use an ordinary method call, but qualify the method name with the package like this:
309
310
311 $fred = Critter-
312 If you're trying to control where the method search begins ''and'' you're executing in the class itself, then you may use the SUPER pseudo class, which says to start looking in your base class's @ISA list without having to name it explicitly:
313
314
315 $self-
316 Please note that the SUPER:: construct is meaningful ''only'' within the class.
317
318
319 Sometimes you want to call a method when you don't know the
320 method name ahead of time. You can use the arrow form,
321 replacing the method name with a simple scalar variable
322 containing the method name or a reference to the
323 function.
324
325
326 $method = $fast ?
327 if ($coderef = $fred-
328
329
330 __WARNING__
331
332
333 While indirect object syntax may well be appealing to
334 English speakers and to C ++ programmers, be
335 not seduced! It suffers from two grave
336 problems.
337
338
339 The first problem is that an indirect object is limited to a
340 name, a scalar variable, or a block, because it would have
341 to do too much lookahead otherwise, just like any other
342 postfix dereference in the language. (These are the same
343 quirky rules as are used for the filehandle slot in
344 functions like print and printf.) This can
345 lead to horribly confusing precedence problems, as in these
346 next two lines:
347
348
349 move $obj-
350 Those actually parse as the very surprising:
351
352
353 $obj-
354 Rather than what you might have expected:
355
356
357 $obj-
358 The left side of ``-
359
360
361 As if that weren't bad enough, think about this: Perl must
362 guess ''at compile time'' whether name and
363 move above are functions or methods. Usually Perl
364 gets it right, but when it doesn't it, you get a function
365 call compiled as a method, or vice versa. This can introduce
366 subtle bugs that are hard to unravel. For example, calling a
367 method new in indirect notation--as C
368 ++ programmers are so wont to do--can be
369 miscompiled into a subroutine call if there's already a
370 new function in scope. You'd end up calling the
371 current package's new as a subroutine, rather than
372 the desired class's method. The compiler tries to cheat by
373 remembering bareword requires, but the grief if it
374 messes up just isn't worth the years of debugging it would
375 likely take you to track such subtle bugs down.
376
377
378 The infix arrow notation using ``-'' doesn't
379 suffer from either of these disturbing ambiguities, so we
380 recommend you use it exclusively.
381
382
383 __Default UNIVERSAL methods__
384
385
386 The UNIVERSAL package automatically contains the
387 following methods that are inherited by all other
388 classes:
389
390
391 isa( CLASS )
392
393
394 isa returns ''true'' if its object is blessed
395 into a subclass of CLASS
396
397
398 isa is also exportable and can be called as a sub
399 with two arguments. This allows the ability to check what a
400 reference points to. Example
401
402
403 use UNIVERSAL qw(isa);
404 if(isa($ref, 'ARRAY')) {
405 #...
406 }
407
408
409 can( METHOD )
410
411
412 can checks to see if its object has a method called
413 METHOD, if it does then a reference to the sub is
414 returned, if it does not then ''undef'' is
415 returned.
416
417
418 VERSION ( [[ NEED ]
419 )
420
421
422 VERSION returns the version number of the class
423 (package). If the NEED argument is given then
424 it will check that the current version (as defined by the
425 $VERSION variable in the given package) not less
426 than NEED ; it will die if this is not the
427 case. This method is normally called as a class method. This
428 method is called automatically by the VERSION form
429 of use.
430
431
432 use A 1.2 qw(some imported subs);
433 # implies:
434 A-
435
436
437 __NOTE:__ can directly uses
438 Perl's internal code for method lookup, and isa
439 uses a very similar method and cache-ing strategy. This may
440 cause strange effects if the Perl code dynamically changes
441 @ISA in any package.
442
443
444 You may add other methods to the UNIVERSAL
445 class via Perl or XS code. You do not need to
446 use UNIVERSAL to make these methods available to
447 your program. This is necessary only if you wish to have
448 isa available as a plain subroutine in the current
449 package.
450
451
452 __Destructors__
453
454
455 When the last reference to an object goes away, the object
456 is automatically destroyed. (This may even be after you
457 exit, if you've stored references in global variables.) If
458 you want to capture control just before the object is freed,
459 you may define a DESTROY method in your
460 class. It will automatically be called at the appropriate
461 moment, and you can do any extra cleanup you need to do.
462 Perl passes a reference to the object under destruction as
463 the first (and only) argument. Beware that the reference is
464 a read-only value, and cannot be modified by manipulating
465 $_[[0] within the destructor. The object itself
466 (i.e. the thingy the reference points to, namely
467 ${$_[[0]}, @{$_[[0]}, %{$_[[0]}
468 etc.) is not similarly constrained.
469
470
471 If you arrange to re-bless the reference before the
472 destructor returns, perl will again call the
473 DESTROY method for the re-blessed object
474 after the current one returns. This can be used for clean
475 delegation of object destruction, or for ensuring that
476 destructors in the base classes of your choosing get called.
477 Explicitly calling DESTROY is also possible,
478 but is usually never needed.
479
480
481 Do not confuse the previous discussion with how objects
482 ''CONTAINED'' in the current one are
483 destroyed. Such objects will be freed and destroyed
484 automatically when the current object is freed, provided no
485 other references to them exist elsewhere.
486
487
488 __Summary__
489
490
491 That's about all there is to it. Now you need just to go off
492 and buy a book about object-oriented design methodology, and
493 bang your forehead with it for the next six months or
494 so.
495
496
497 __Two-Phased Garbage Collection__
498
499
500 For most purposes, Perl uses a fast and simple,
501 reference-based garbage collection system. That means
502 there's an extra dereference going on at some level, so if
503 you haven't built your Perl executable using your C
504 compiler's -O flag, performance will suffer. If you
505 ''have'' built Perl with cc -O, then this
506 probably won't matter.
507
508
509 A more serious concern is that unreachable memory with a
510 non-zero reference count will not normally get freed.
511 Therefore, this is a bad idea:
512
513
514 {
515 my $a;
516 $a = $a;
517 }
518 Even thought $a ''should'' go away, it can't. When building recursive data structures, you'll have to break the self-reference yourself explicitly if you don't care to leak. For example, here's a self-referential node such as one might use in a sophisticated tree structure:
519
520
521 sub new_node {
522 my $self = shift;
523 my $class = ref($self) $self;
524 my $node = {};
525 $node-
526 If you create nodes like that, they (currently) won't go away unless you break their self reference yourself. (In other words, this is not to be construed as a feature, and you shouldn't depend on it.)
527
528
529 Almost.
530
531
532 When an interpreter thread finally shuts down (usually when
533 your program exits), then a rather costly but complete
534 mark-and-sweep style of garbage collection is performed, and
535 everything allocated by that thread gets destroyed. This is
536 essential to support Perl as an embedded or a
537 multithreadable language. For example, this program
538 demonstrates Perl's two-phased garbage
539 collection:
540
541
542 #!/usr/bin/perl
543 package Subtle;
544 sub new {
545 my $test;
546 $test = $test;
547 warn
548 sub DESTROY {
549 my $self = shift;
550 warn
551 package main;
552 warn
553 warn
554 When run as ''/tmp/test'', the following output is produced:
555
556
557 starting program at /tmp/test line 18.
558 CREATING SCALAR(0x8e5b8) at /tmp/test line 7.
559 CREATING SCALAR(0x8e57c) at /tmp/test line 7.
560 leaving block at /tmp/test line 23.
561 DESTROYING Subtle=SCALAR(0x8e5b8) at /tmp/test line 13.
562 just exited block at /tmp/test line 26.
563 time to die... at /tmp/test line 27.
564 DESTROYING Subtle=SCALAR(0x8e57c) during global destruction.
565 Notice that ``global destruction'' bit there? That's the thread garbage collector reaching the unreachable.
566
567
568 Objects are always destructed, even when regular refs
569 aren't. Objects are destructed in a separate pass before
570 ordinary refs just to prevent object destructors from using
571 refs that have been themselves destructed. Plain refs are
572 only garbage-collected if the destruct level is greater than
573 0. You can test the higher levels of global destruction by
574 setting the PERL_DESTRUCT_LEVEL environment
575 variable, presuming -DDEBUGGING was enabled during
576 perl build time.
577
578
579 A more complete garbage collection strategy will be
580 implemented at a future date.
581
582
583 In the meantime, the best solution is to create a
584 non-recursive container class that holds a pointer to the
585 self-referential data structure. Define a
586 DESTROY method for the containing object's
587 class that manually breaks the circularities in the
588 self-referential structure.
589 !!SEE ALSO
590
591
592 A kinder, gentler tutorial on object-oriented programming in
593 Perl can be found in perltoot, perlbootc and perltootc. You
594 should also check out perlbot for other object tricks,
595 traps, and tips, as well as perlmodlib for some style guides
596 on constructing both modules and classes.
597 ----
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.