Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

trap signal 'exit': why I am not able to have it work??

by alex5161 (Novice)
on Oct 30, 2017 at 22:32 UTC ( [id://1202383]=perlquestion: print w/replies, xml ) Need Help??

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

First time trying to work with signals in Perl.
Reviewing example I try it, but not able to get it work for 'exit'.
I hope, I am correct, assuming, that the ending any code by

exit $return_code;

the $SIG{EXIT} should be de-referenced and processed?!
So, I have such code, that, I assume, should, but does not process signal handling:

#!/bin/perl sub trp_h{ print "\nIn 'trp_h()'. ". "\nReceived parameters \@_: ".(join ", ",@_). "\n\$! as a number: ".($!+0).", as a string: ".(" ".$!). "\n now 'sleep 5' before return."; sleep 5; } $SIG{EXIT}='trp_h'; print "\nSet handler: $SIG{EXIT}\n"; exit(5); print "after first exit"; $SIG{EXIT}=\&trp_h; print "\nSet handler to code: $SIG{EXIT}\n"; exit(3); print "after second exit\n"; print "Restoring to default\n"; $SIG{EXIT}='DEFAULT'; exit(2); exit 1;

Please, help me understand what is wrong, or,
if I mistaken on assumption that the 'exit()' is processed by the $SIG{EXIT},
how it could be handled to process activity, such as in UNIX 'trap "..." EXIT' command?

Thanks!

Replies are listed 'Best First'.
Re: trap signal 'exit': why I am not able to have it work??
by choroba (Cardinal) on Oct 30, 2017 at 23:01 UTC
    There's no EXIT handler in Perl. You can use the END {...} block instead. Why do you need it?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Sure!

      Completely cover all my needs! Just followed shell-script functionality and did not guess about the END{}

      Thanks!

        Note that END blocks are executed in the reverse order they are "registered". If you want or need your END block to run last, you need to put it before any use directives (though should be safe to put it after strict and warnings).

        use strict; use warnings; my $exit_normally = 0; sub exit_handler; END { exit_handler; } # other use directives sub exit_handler { ... } ... $exit_normally = 1; __END__
Re: trap signal 'exit': why I am not able to have it work?? (updated)
by LanX (Saint) on Oct 30, 2017 at 22:57 UTC
    I have trouble reading your post, please don't use <pre> tags for normal text.

    update

    From my understanding are handlers %SIG supposed to process external (OS specific) signals send from a kill command to the process, maybe with the only exception of warn and die which are not signals but internal hooks.

    see also perlipc#Signals

    I couldn't find any mention of the possibility to intercept exit() with a sighandler

    But this "trapping exit() calls with %SIG" explicitly shows how to execute code in an END block after exit .

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Sorry for uncomfortability of < pre >, but on < p > I do not like big intervls; without anything - I hate spagety lines...

      Yes, the END{} completely fine with me.

      Thanks!

        > Sorry for uncomfortability of < pre >, but on < p > I do not like big intervls; without anything - I hate spagety lines...

        And the monastery hates <pre> tags and you are free to use <br> tags ... :)

        please see Markup in the Monastery

        Tags You Should NOT Use

        At first blush, the <pre> ...</pre> pair may look like an alternative to code tags... BUT DON'T USE IT HERE chiefly because <pre>...</pre> tags will not persuade the Monastery to provide a download link for the contents of the <pre> ... and while it is now supposed to wrap lines that are too long for the viewer's browser window, there are ways Monks could use CSS that would defeat that. So, please. don't use <pre>...</pre> .

        If you want I wrote a "wiki" add on for the monastery in JS facilitating the creation of posts (especially on mobiles)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Re: trap signal 'exit': why I am not able to have it work??
by QM (Parson) on Oct 31, 2017 at 10:05 UTC
    A google search for "perl signal handler exit" gave this SOPM node, and the first answer also says use END{}.

    Of course, going to the next level, you can't switch "exit" handlers midstream, and you can't unregister END{} blocks, AFAIK. I suppose there would have to be only one END{} block, with a dynamic call table for subs, and the call table gets changed as needed during the program execution. (Maybe this is proper class material, with methods to mangle the call table.)

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

    http://www.perlmonks.org/?node_id=849549
      you can't switch "exit" handlers midstream, and you can't unregister END{} blocks,

      But you can enable or disable code in the END blocks by using flag variables:

      >cat demo.pl #!/usr/bin/perl use v5.12; use warnings; our $endFlag; END { if ($endFlag) { say "End flag is set, have to so some cleanup"; say "Wash, rinse, repeat" for 1..5; } else { say "Nothing to do"; } } $endFlag = $ARGV[0]; say "How will this end?"; >perl demo.pl 0 How will this end? Nothing to do >perl demo.pl 1 How will this end? End flag is set, have to so some cleanup Wash, rinse, repeat Wash, rinse, repeat Wash, rinse, repeat Wash, rinse, repeat Wash, rinse, repeat >

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Thanks; th END{} works; just did not guessed!

      And, sure, conditional execution in END{} useful! (Almost the same: for %SIG change a line of code needed; so, for a exit_condition, also, just a line!)
Re: trap signal 'exit': why I am not able to have it work??
by holli (Abbot) on Oct 30, 2017 at 23:20 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found