Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

DESTROY on CTRL+C

by gri6507 (Deacon)
on Aug 29, 2013 at 12:37 UTC ( [id://1051418]=perlquestion: print w/replies, xml ) Need Help??

gri6507 has asked for the wisdom of the Perl Monks concerning the following question:

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";

Replies are listed 'Best First'.
Re: DESTROY on CTRL+C
by McA (Priest) on Aug 29, 2013 at 12:46 UTC

    Put this in front of your script:

    $SIG{INT} = sub { print "I'll die\n"; exit 1; };

    and try it again. You'll get the building blocks.

    UPDATE: In http://perldoc.perl.org/perlvar.html search for SIG.

    UPDATE2: Have a look at File::Temp. File system handling on unixoid operating systems do have a nice feature. You can open a file (result is a opened filehandle) and directly afterwards unlink the file. This has the result that you have a file you can write to and read from but which can't be seen in the directory listing. The better: As soon as you close the file explicitly in your program or implicitly by killing the program the file gets deleted as there is no reference to it anymore. IMHO this would be the right approach in your object.

    McA

      Update: I wrote: ...

      in front of your script

      anywhere in your script should be fine.

      ...but that was a misconception. The %SIG hash is not all that magical.
      McA is right, it must be writ before the actual code to be affected by the signal.

      Cheers, Sören

      Créateur des bugs mobiles - let loose once, run everywhere.
      (hooked on the Perl Programming language)

        Thanks, that's kind of what I suspected, but wanted to confirm. I am a bit surprised though that a SIG{INT} does not automatically cause the DESTROY functions to be called. Just for completeness sake, does a SIG{INT} cause the END block to execute? P.S. I actually am using File::Temp in my code to generate the temporary file. However, because of system constraints, I cannot rely on any temporary directory other than pwd, and according to the documentation:
        Note that if a temp directory is your current directory, it cannot be removed. chdir() out of the directory first before calling cleanup(). (For the cleanup at program exit when the CLEANUP flag is set, this happens automatically.)

        Yup, that's more precise...thumbs up, äähm no, ++. ;-)

        I love my wife... ;-)

        I like TIMTOWTDI.

        But seriously. Is there an advantage? Just overlooked this pragma.

        McA

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1051418]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found