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