Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by QM (Parson)
on Oct 31, 2017 at 10:05 UTC ( #1202405=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^2: trap signal 'exit': why I am not able to have it work??
by afoken (Canon) on Oct 31, 2017 at 12:50 UTC
    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". ;-)
Re^2: trap signal 'exit': why I am not able to have it work??
by alex5161 (Novice) on Oct 31, 2017 at 13:25 UTC

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1202405]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2021-04-16 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?