Your last solution is problematic; running
use strict; use warnings; my $timed_out = 1; TIMEOUT_BLOCK_WITH_UNIQUE_LABEL: { local $SIG{ALRM} = sub { last TIMEOUT_BLOCK_WITH_UNIQUE_LABEL; }; my $err; eval { alarm( 1 ); do_work(); 1 } or do { $err = $@ || 'Unknown error'; }; alarm( 0 ); die $err if $err; $timed_out = 0; } sub do_work { my $var = 0; for (1 .. 1000000) { for (1 .. 1000000) { $var++; } } }
outputs
Exiting subroutine via last at fluff.pl line 38. Exiting subroutine via last at fluff.pl line 38. Exiting eval via last at fluff.pl line 38. Label not found for "last TIMEOUT_BLOCK_WITH_UNIQUE_LABEL" at fluff.pl + line 38.
for me. I've been trying (unsuccessfully so far) to implement educated_foo's goto solution as well. The LABEL search algorithm seems to fail when invoked in a signal handling context:
use strict; use warnings; local $SIG{ALRM} = sub { goto LABEL1; }; alarm 1; my $var = 0; for (1 .. 1000000) { for (1 .. 1000000) { $var++; } } print "Here\n"; LABEL1: 1; print "There\n";
vs.
use strict; use warnings; sub go { goto LABEL1; } go(); print "Here\n"; LABEL1: 1; print "There\n";

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re^2: die through several evals (last) by kennethk
in thread die through several evals by nyaapa

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.