Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical


by McA (Priest)
on Aug 29, 2013 at 12:46 UTC ( #1051422=note: print w/replies, xml ) Need Help??

in reply to DESTROY on CTRL+C

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 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.


Replies are listed 'Best First'.
by Happy-the-monk (Abbot) on Aug 29, 2013 at 12:50 UTC

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

        I'm not sure what you mean. As soon as the SIG{INT} handler is installed and I press CTRL-C while your program sleeps I can see the output of the sig handler ('Delete') and the output of the DESTROY method ('In DESTROY()').

        To your second question:

        #!/usr/bin/env perl use strict; use warnings; use 5.010; $SIG{INT} = sub { say "Caught a signal"; exit 1; }; say "Program started"; say "Starting to sleep"; sleep 3; say "I've been sleeping long enough"; END { say "I'm in the END block"; }

        This code should give you confidence. In my case (Linux) the END block is executed.

        I'm not sure whether you mix up the creation of a temp directory and temp file. You can create a temp file in your pwd and unlink it with:

        my ($fh, $filename) = tempfile($template, DIR => '.'); unlink $filename;

        As long as $fh is open you will have a file to write to and read from.

        Have a look at:

        #!/usr/bin/env perl use strict; use warnings; use 5.010; use Cwd (); use Fcntl (); use File::Temp qw(tempfile); my $pwd = Cwd::getcwd; say "pwd: $pwd"; my $template = 'somthing.XXXXXXX'; my $dir = '.'; my ($fh, $filename) = tempfile($template, DIR => $dir); say "filename: $filename"; system("ls -l '$filename'"); unlink $filename; system("ls -l '$filename'"); say "Now store 'Put some text in' in the file"; print $fh "Put some text in\n"; say "Written to file, try to read"; seek $fh, Fcntl::SEEK_SET, 0; while(my $line = <$fh>) { print "Found in file: $line"; } close $fh; say "End of prog";


        END is executed if you have SIG handler, and not executed if you have no.

        An END code block is executed as late as possible, that is, after perl has finished running the program and just before the interpreter is being exited, even if it is exiting as a result of a die() function. (But not if it's morphing into another program via exec, or being blown out of the water by a signal--you have to trap that yourself (if you can).)

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

by daxim (Chaplain) on Aug 29, 2013 at 12:58 UTC

      I love my wife... ;-)

      I like TIMTOWTDI.

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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1051422]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-02-19 06:55 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (259 votes). Check out past polls.