Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Automatically deleting selected directories on program exit

by eyepopslikeamosquito (Canon)
on May 13, 2005 at 09:49 UTC ( #456650=perlquestion: print w/ replies, xml ) Need Help??
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

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;
and here is an example program that uses it:
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.

Comment on Automatically deleting selected directories on program exit
Select or Download Code
Re: Automatically deleting selected directories on program exit
by gaal (Parson) on May 13, 2005 at 10:11 UTC
    Not commenting on the technique itself, but on the usage of delete_on_close: I try to avoid Fully::Qualified::functions() in OO code because mixing them up with inheritable class methods is such an easy source for bugs. That is, I almost *always* use a class method and call it with Class->meth() syntax.

    This code is actually an example where the change isn't absolutely trivial, because $On_close is a file-scoped lexical. I'd put it in a tighter scope and write an accessor (or for smaller stuff cop out and make it our).

Re: Automatically deleting selected directories on program exit
by merlyn (Sage) on May 13, 2005 at 10:14 UTC
    Looks a lot like the CLEANUP option of the tempdir function of File::Temp, which reads:
    $tempdir = tempdir( $template, CLEANUP => 1); Create a temporary directory using the supplied template, b +ut attempt to remove it (and all files inside it) when the pro +gram exits. Note that an attempt will be made to remove all file +s from the directory even if they were not created by this module +(other- wise why ask to clean it up?). The directory removal is mad +e with the rmtree() function from the File::Path module. Of cours +e, if the template is not specified, the temporary directory will + be cre- ated in tmpdir() and will also be removed at program exit.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Thanks merlyn. With my inherited legacy code, I'd prefer not to change the existing interface. And the directories to be deleted are not "temporary" but are supplied by the caller with specific names (so I can't see any way of using File::Temp directly). However, looking at the File::Temp implementation of _deferred_unlink() has allowed me to simplify the implementation as follows:

      package MyTemp; use strict; use warnings; use File::Path (); my @dirs_to_delete; sub delete_on_close { push(@dirs_to_delete, @_) } END { print "Deleting dirs:\n"; print " $_\n" for @dirs_to_delete; File::Path::rmtree(\@dirs_to_delete); } 1;

Re: Automatically deleting selected directories on program exit
by hv (Parson) on May 13, 2005 at 10:15 UTC

    Hmm, I'd be somewhat concerned about this, purely because it is relying on effects during global destruction - I don't know exactly what order things happen during this phase, but based on bug reports I've seen it seems possible that in some cases some or all of the strings in the $on_close arrayref may get destroyed before $on_close itself.

    If I remember correctly, the symptom would be "use of uninitialised value ... during global destruction", and one or more directories would not be cleaned up.

    However I do use DESTROY effects on scope exit a lot - if you were to wrap a block around the main program and declare $On_close within that block I'd have every confidence in it.


Re: Automatically deleting selected directories on program exit
by polettix (Vicar) on May 13, 2005 at 10:20 UTC
    If I read well, the programmer is quite scared of someone breaking the interface - otherwise I don't understand why using this double-interface MyTemp/_Knob. I'd probably trust the user a little bit more and stick to _Knob directly, but that's a matter of taste.

    Given the fact that it deals with temporary directories, I'd go a little further and encapsulate directory creation inside as well. In this way, you would reduce the risk that you create a temporary directory but forget to mark it as mortal (if you pass me the XS language). This makes me wonder if there's a CPAN module that already does this - if not, it's a good idea IMHO.

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
      This makes me wonder if there's a CPAN module that already does this - if not, it's a good idea IMHO.

      Like File::Temp, which has been part of the core since before the Dawn of Time?

      • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
      • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
        I knew it was a da*n1 good idea! :)

        1Dawn, of course.

        Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

        Don't fool yourself.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://456650]
Approved by Thilosophy
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2014-07-13 06:26 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (247 votes), past polls