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

From the documentation:
tempdir

This is the recommended interface for creation of temporary directories. By default the directory will not be removed on exit (that is, it won't be temporary; this behaviour can not be changed because of issues with backwards compatibility). To enable removal either use the CLEANUP option which will trigger removal on program exit, or consider using the newdir method in the object interface which will allow the directory to be cleaned up when the object goes out of scope.

So the important part of the documentation you cited was that are registered for removal. The directory your code created was not registered for removal. To register it, create it with

my $x = tempdir(CLEANUP => 1);

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

Replies are listed 'Best First'.
Re^2: File::Temp does not cleanup() on demand
by bliako (Abbot) on Oct 05, 2023 at 13:36 UTC

    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.

      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!)

      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.