in reply to Re: die through several evals (last)
in thread die through several evals
outputsuse 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++; } } }
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: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.
vs.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";
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: die through several evals (last words)
by tye (Sage) on Apr 23, 2013 at 20:12 UTC | |
by nyaapa (Novice) on Apr 24, 2013 at 09:27 UTC |