in reply to Re: File::Temp does not cleanup() on demand
in thread File::Temp does not cleanup() on demand

Thanks choroba. This is part of a test file in which I sometimes want to keep the temp dirs and sometimes to delete them. I want to keep them when the test fails and I need to inspect the temp dirs for debugging info. I want the temp dirs to be erased only when test is successful. So I imagined I could just call cleanup() at the end.

Replies are listed 'Best First'.
Re^3: File::Temp does not cleanup() on demand
by choroba (Cardinal) on Oct 05, 2023 at 13:40 UTC
    Then maybe use the CLEANUP => 1 but set $KEEP_ALL = 1 when a test fails.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      choroba++ your suggestion does it

      use strict; use warnings; use File::Temp 'tempdir'; $File::Temp::KEEP_ALL = 1; my $x = tempdir(CLEANUP=>1); print "tmp $x\n"; # exit(1); # this will retain it # ... # on successful end of test done_testing; $File::Temp::KEEP_ALL = 0; File::Temp::cleanup(); if( -d $x ){ print "still there ($x) ...\n" }

      thanks a lot (for reading the whole manual!)

Re^3: File::Temp does not cleanup() on demand
by swl (Prior) on Oct 05, 2023 at 21:48 UTC

    Given the use case, it's worth looking at Test::TempDir::Tiny as it would seem to do exactly what you want with a clean interface. (Edit - it wraps File::Temp so is essentially the same as 11154828 but more concise).

    I've been using it for a while and it works pretty well, barring issues deleting directories when antivirus software is scanning files when the module tries to delete them. That issue is not unique to Test::TempDir::Tiny, though.