Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2016-05-28 05:33 GMT
Find Nodes?
    Voting Booth?