http://www.perlmonks.org?node_id=8204


in reply to How do I make a destructor?

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.