Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Answer: How do I make a destructor?

by chromatic (Archbishop)
on Apr 20, 2000 at 20:56 UTC ( #8204=categorized answer: print w/replies, xml ) Need Help??

Q&A > object-oriented programming > How do I make a destructor? - Answer 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.

Log In?

What's my password?
Create A New User
and the fog begins to lift...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2018-05-24 18:42 GMT
Find Nodes?
    Voting Booth?