13.2. Destroying an ObjectProblemYou want to run special code whenever an object is no longer used. This is sometimes needed when the object is an interface to the outside world - or contains circular data structures - and must clean up after itself. You might remove temporary files, break circular links, gracefully disconnect from a socket, or kill a spawned subprocess. Solution
Create a method named sub DESTROY { my $self = shift; printf("$self dying at %s\n", scalar localtime); } Discussion
Every story has a beginning and an end. The beginning of the object's story is its constructor, explicitly called when the object comes into existence. The end of its story is the
destructor
, a method implicitly called when an object leaves this life. Any per-object clean-up code is placed in the destructor, which must be named
Why can't destructors have arbitrary names? Because, although a constructor is explicitly called by name, a destructor is not. Destruction happens automatically via Perl's
garbage collection (GC) system, which is currently implemented as a quick but lazy reference-based GC system. To know what to call, Perl insists that the destructor be named
Why is The user doesn't care when the destructor is called. It just happens when it's supposed to. In languages without any form of GC, this is undependable, so the programmer must explicitly call the destructor to clean up memory and state, crossing their fingers that it's the right time to do so. This is a terrible state of affairs. Because of Perl's automatic memory management, an object destructor is rarely needed in Perl. Even when it is, explicit invocation is not only uncalled for, it's downright dangerous. The destructor will be called by the run-time system when the object is no longer in use. Most classes don't need a destructor because Perl takes care of simple matters like memory deallocation. The only situation where Perl's reference-based garbage collection system won't work for you is when there's a circularity in your data structure, such as: $self->{WHATEVER} = $self; In that case, you must delete the self-reference manually if you expect your program not to leak memory. While admittedly error-prone, this is the best we can do right now. Recipe 13.13 provides an elegant solution to this problem, however. Nonetheless, rest assured that when your program is finished, its objects' destructors are all duly called. At interpreter shutdown time, a second, more sweeping form of garbage collection is performed. Not even unreachable or circular objects can escape this final destruction. So you are guaranteed that an object eventually gets properly destroyed, unless a program never exits. If you're running Perl embedded in another application, the second GC pass happens more frequently - whenever an interpreter shuts down.
See Alsoperltoot (1) and perlobj (1); the section "A Note on Garbage Collection" in Chapter 5 of Programming Perl ; Recipe 13.10 ; Recipe 13.13 |
|