[Gllug] Timeouts in perl

- Tethys tethys at gmail.com
Tue Oct 31 17:01:21 UTC 2006


I'm getting increasingly frustrated with the braindead mess of
rebarbative syntax that is perl. However, since I know many of you use
it, perhaps one of you can come to my rescue. I need to time out long
running operations. Rather than explicitly code a timeout in each
function (and there are many), I'm trying to write a generic timeout
wrapper. It goes something like this:

sub sigalrm_handler
{
    our $timed_out = 1;
    die "timeout";
}

sub timeout
{
    my ($timeout, $func, $arg) = @_;

    our $timed_out = 0;
    eval
    {
        local $SIG{ALRM} = \&sigalrm_handler;
        alarm $timeoutsecs;

        $func -> ($arg);
        alarm 0;
    };
    alarm 0;
    if ($timed_out == 1) { print "timeout occured\n"; }
}

The handler is definitely being called (verified by adding a printf to
it). However, the exception triggered by the die call is not being
caught by the eval. Do I need to do something explicit to propagate
the exception up the calling stack? It works if the code is inside the
eval itself, rather than in a separate function called from within the
eval. But that tends to default the whole point of the exercise. I
can't find anything about it in the camel book.

TIA,

Tet
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list