eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Automatically deleting selected directories on program exit
by merlyn (Sage) on May 13, 2005 at 10:14 UTC | |
by eyepopslikeamosquito (Archbishop) on May 16, 2005 at 05:03 UTC | |
|
Re: Automatically deleting selected directories on program exit
by gaal (Parson) on May 13, 2005 at 10:11 UTC | |
|
Re: Automatically deleting selected directories on program exit
by hv (Prior) on May 13, 2005 at 10:15 UTC | |
|
Re: Automatically deleting selected directories on program exit
by polettix (Vicar) on May 13, 2005 at 10:20 UTC | |
by dragonchild (Archbishop) on May 13, 2005 at 12:54 UTC | |
by polettix (Vicar) on May 13, 2005 at 15:44 UTC |