InfiniteLoop has asked for the wisdom of the Perl Monks concerning the following question:

Greeting Monks,

   I have written a perl module that operates on files. Also I have a Test::More script for this module, with number of tests.
One of the test requires a temporary file. I create this temporary file in a BEGIN {} block. I unlink() it in a DESTROY {} block.

  If the test passes, the temporary file is unlink()'d. However if the test fails, the temporary file is not unlink()'d. How do i unlink the temporary file, even if the test fails ?

Replies are listed 'Best First'.
Re: cleanup Test::More
by dave_the_m (Monsignor) on Oct 08, 2004 at 10:25 UTC
    Unlink it in an END block ?

    Dave.

      Thanks. In hind sight the question was pretty short sighted :).
Re: cleanup Test::More
by thospel (Hermit) on Oct 08, 2004 at 11:16 UTC
    I like to use the (nowadays) builtin File::Temp module for that. It allows you to ask for temporary files (and whole directory trees) that get autmatically cleaned up and also allows you to portably have these files in an appropiate temporary area so you don't have to assume too much about the writability of the place where the module source resides and is being tested.
Re: cleanup Test::More
by PodMaster (Abbot) on Oct 08, 2004 at 13:20 UTC
    portability tip: some operating systems don't let you delete directories you are chdired to, so its not a bad idea to  chdir File::Spec->updir; once or twice in an END{} block

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.