Fellow monks,
I am writing a module which, upon instantiation creates a temporary file. I then try to unlink() that file from the computer when the program terminates using the DESTROY method within this module. This all works when execution ends normally. However, whenever the program execution is aborted from CLI using <ctrl>+c, the temporary file remains.
Do I need to do something special to handle abnormal exits? Here is my distilled down code.
package foo;
use warnings;
use strict;
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {@_};
bless($self, $class);
$self->{file} = 'bar';
open my $handle, ">", $self->{file} || die "Cannot write to $self-
+>{file}: $!\n";
print $handle "FooBar";
close $handle;
return $self;
}
sub DESTROY {
my $self = shift;
print "In DESTROY()\n";
unlink $self->{file};
}
1;
use warnings;
use strict;
$|++;
my $f = foo->new();
print "Sleeping for 5 sec ... ";
sleep(5);
print "Done\n";