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