I just inherited a bunch of code and noticed an interesting way to automatically delete a specified set of directories when the program terminates. I am posting it here to get confirmation that it is using a sound technique. Plus, I'm interested in seeing alternative and better ways to do it.
Here is the file MyTemp.pm:
and here is an example program that uses it: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.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |