in reply to Automatically deleting selected directories on program exit

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.

Replies are listed 'Best First'.
Re^2: Automatically deleting selected directories on program exit
by eyepopslikeamosquito (Archbishop) on May 16, 2005 at 05:03 UTC

    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;