Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: die through several evals (last)

by kennethk (Monsignor)
on Apr 23, 2013 at 17:49 UTC ( #1030203=note: print w/ replies, xml ) Need Help??


in reply to Re: die through several evals (last)
in thread die through several evals

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.


Comment on Re^2: die through several evals (last)
Select or Download Code
Re^3: die through several evals (last words)
by tye (Cardinal) on Apr 23, 2013 at 20:12 UTC
    The LABEL search algorithm seems to fail when invoked in a signal handling context

    Ah, that's very good information to have.

    That idea was hackish enough for me that I've never actually tried to use it. Thanks for testing it.

    Here's a crazy idea I'd never use myself, either:

    { package Medusa; # Fatal to look at use Carp 'croak'; use overload( '""' => sub { die $_[0] } ); sub new { my $self = 0; return bless \$self; } } my $timed_out = 0; eval { local $SIG{ALRM} = sub { warn "Time's up!\n"; die Medusa->new(); }; alarm( $seconds ); do_work(); alarm( 0 ); 1; } or do { alarm( 0 ); die $@ if "Medusa" ne ref($@); $@ = ''; $timed_out = 1; };

    It works when tested on:

    sub do_work { eval { eval { my $word = $ENV{WORD} || 'a'; $word++ while $word ne 'interrupt'; warn "Got: $word\n"; }; if( $@ ) { warn "Ignoring failure.\n"; } 1 } or do { my $e = $@ || 'Unknown error'; warn "Failed: $e\n"; }; } my $seconds = 2;

    But that isn't a guarantee that every 'eval' will try to look at $@ after it fails. :)

    - tye        

      very good idea, but i have a guarantee, that sometimes they ignore error-code checking =(

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2014-09-18 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (108 votes), past polls