[Gllug] Timeouts in perl

Iain Gray iain.dv at googlemail.com
Wed Nov 1 10:06:04 UTC 2006


I had to look up rebarbative - good word - but perhaps a less
combative request for help might work.
http://search.cpan.org/~patl/Time-Out-0.05/Out.pod

is almost certainly what you want.

Iain


On 31/10/06, - Tethys <tethys at gmail.com> wrote:
> 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
>
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list