package MyTemp; use strict; use warnings; my $On_close = _Knob->new(); sub delete_on_close { $On_close->add(@_) } package _Knob; use File::Path (); sub new { my $class = shift; bless([], $class); } sub add { my $self = shift; push(@{$self}, @_); } sub DESTROY { my $self = shift; print "Deleting dirs:\n"; print " $_\n" for @{$self}; File::Path::rmtree(\@{$self}); } 1; #### use strict; use warnings; use MyTemp; MyTemp::delete_on_close('this-dir-tmp'); MyTemp::delete_on_close('that-dir-1-tmp', 'that-dir-2-tmp'); # do stuff... # Directories this-dir-tmp, that-dir-1-tmp, that-dir-2-tmp are # automatically deleted on program exit.