Note that there are some explanatory texts on larger screens.

plurals
  1. POperl: handle die before the framework
    primarykey
    data
    text
    <p>I am working with a perl framework which monitor $SIG{<strong>DIE</strong>} itself, my code was executed by the framework, so my exception handle code cannot be executed because the framework is first one to detected the exception then terminate the script.</p> <p>frame.pm</p> <pre><code>sub execute { $SIG{__DIE__} = \&amp;_handleDie; eval{ #execute myscript.pl sub main $rv = &amp;$pFunct(@args);} if ($@){ processException($@)} print "myscript.pl success executed" } </code></pre> <p>myscript.pl</p> <pre><code>use frame; frame-&gt;execute( \&amp;main ); sub main { %codes that redirect STDOUT to a file% #if below API cmd no exception, hide it's output, #otherwise output the API cmd STDERR msg %codes called API of another module% try { die("sth wrong"); }catch{ %codes restore STDOUT to terminal% print "error msg, but this line will not be executed, how to get it be execute?" } } </code></pre> <p>The script first redirect STDOUT to a file for dumy some no use output.</p> <p>When I want to implement is if exception happen(die line), the script can restore STDOUT to terminal then print error to terminal. Now it was handled by frame and print to STDOUT but not STDERR, so I need to handle restore STDOUT before frame print it to STDOUT.</p> <p>with ruakh's solution, myscript.pl has passed SIG of frame, now catched by frame line if ($@){ processException($@)}, <strong>that is when execute myscript->die(), the program come to frame->if ($@){ processException($@)}, but not myscript->catch</strong></p> <p>=====================</p> <p>I finally found this works for me:</p> <p>myscript.pl</p> <pre><code>frame-&gt;execute( \&amp;main ); sub main { open my $stdOri, "&gt;&amp;STDOUT"; my $tmpFile = "/tmp/.output.txt.$$"; open STDOUT, "&gt;$tmpFile"; #overwrite frame provided exception handling. local $SIG{__DIE__}=sub{close STDOUT; open STDOUT, "&gt;&amp;", $stdOri;}; #cause a exception, #this exception will be processed by 'local $SIG{__DIE__}' block which restore STDOUT #then frame-&gt;eval catch this exception, and print it in the terminal. my $c=5/0; } </code></pre> <p>thanks for ruakh's inspire.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload