Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Answer: How do I make a destructor?

( #8204=categorized answer: print w/ replies, xml ) Need Help??

Q&A > object-oriented programming > How do I make a destructor? contributed by chromatic

Create a method named DESTROY. As in other languages, if you need to close a filehandle, clean up a temporary file, release some resource, or break a circular reference, here is where you do it. Perl will call an object's DESTROY method when the object goes out of scope, right before it is garbage collected. Usually, it isn't necessary to define your own, but in the interest of completeness:
package CowboyNeal; sub DESTROY { my $self = shift; print ref($self), " is going away. It was known as ", $self->name +(), " back in the day.\n"; }
This would be more useful if CowboyNeals were circular lists that needed to be broken, as in this case:
sub DESTROY { my $self = shift; $self->{first} = undef; $self->{last} = undef; }
But that's just downright nutty.

Comment on Answer: How do I make a destructor?
Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-08-29 22:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls