update: Take a look in the module Die::Alive. It does exactly what the code below does.

You always can put your code inside an eval, but if you want to really make die() work like warn(), even for outside code, and keep the app running, you need to overwrite the die() function.

Soo, here I show you an example of how to do that, and note that the BEGIN code, need to be loaded before compile any other code. Soo, this BEGIN code need to be the first thing in your script, since what is compiled before overwrite the die() function, won't keep the overwrite reference:

sub BEGIN { *CORE::GLOBAL::die = \&DIE_2_WARN ; $SIG{__DIE__} = \&DIE_2_WARN ; } sub DIE_2_WARN { warn(@_) ;} die("This die() won't exit!") ; print "And here we continue the app...\n" ; eval { die("die inside eval!"); print "Won't go out of eval!\n" ; } ;
As we can see, if you make die() work like warn(), a die() that is made inside an eval() won't work like generally we want, that is to go out of the eval. Soo, we need to handle that too, using the predefined variable $^S:
sub BEGIN { *CORE::GLOBAL::die = \&DIE_2_WARN ; $SIG{__DIE__} = \&DIE_2_WARN ; } sub DIE_2_WARN { if ( $^S ) { CORE::die(@_) ;} else { warn(@_) ;} } die("This die() won't exit!") ; print "And here we continue the app...\n" ; eval { die("die inside eval!"); print "And this print won't be executed.\n" ; } ; print "ERROR: $@\n" ;
Enjoy!

Graciliano M. P.
"Creativity is the expression of the liberty".


In reply to Re: Keeping perl alive after a module calls die. (making die() work like warn()) by gmpassos
in thread Keeping perl alive after a module calls die. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.