$File::Temp::KEEP_ALL = 0;
File::Temp::cleanup;
####
use strict;
use warnings;
use Capture::Tiny qw(capture);
use File::Temp 'tempdir';
$File::Temp::KEEP_ALL = 1;
my $tmpdir = File::Temp::tempdir();
print "at1: ";system("ls /tmp | wc -l");
my ($stdout, $stderr) = capture {
# do some system command
};
# now there are two more temp files in /tmp
# they should have not been there!
print "at2: ";system("ls /tmp | wc -l");
$File::Temp::KEEP_ALL = 0;
File::Temp::cleanup;
# they are still there
print "at3: ";system("ls /tmp | wc -l");
####
...
If the global variable $KEEP_ALL is true, the file or directory will not be removed.