I am creating temp files with File::Temp. My temp files are deleted automatically when the program exits normally. I expected the same to happen when my program is stopped with ctrl+c. It is not.
Here’s a simple program demonstrating my problem.
The desired behavior is this. How can I achieve it? I am on Linux.
- temp file is not deleted when closing the file handle (ok)
- temp file gets deleted when program exits normally (ok)
- temp file gets deleted when program exits with ctrl+c (not working)
.
#!/usr/bin/perl -l
use warnings; use strict;
use File::Temp qw(tempfile tmpfile);
my $fh = File::Temp->new;
close $fh;
-f "$fh" || die "$fh does not exist";
print "hit enter and the file will be deleted";
print "hit ctrl+c and it won't";
print "verify with ls $fh after program exits";
readline STDIN;
EDIT 1
I tested the behavior of tempfile. It seems to confirm that Linux supports what I am looking for (marking files as temp / unlinking open file). I seem to have idealized File::Temp somehow.
# program will die because os deletes tmpfile after close
my $fh = tempfile;
close $fh;
stat $fh || die;
readline STDIN;
To cause a Control_C keyboard interrupt to run your
ENDblock you will need to add a signal handler. Compare the behavior of:with:
From the perlmod