Okay, I did a simple rewrite using my Alarm::Concurrent module. You can add ':OVERRIDE' just after 'setalarm' in the use line (line # 3) to have it take over $SIG{ALRM} if you like.

Let me know what you think. :-)

#!/usr/bin/perl -w use Alarm::Concurrent qw( setalarm ); my $TIMELEVEL = 0; sub timeout(&;$) { my $code = shift; my $time = shift || 30; my $dietext = "Timeout " . ++$TIMELEVEL . "\n"; eval { local $SIG{__DIE__}; setalarm($time, sub { die $dietext }); $code->(); }; --$TIMELEVEL; if($@ eq $dietext) { return 0; # Timed out. } elsif($@ ne '') { die $@; # Propagate error upstream. } return 1; # Everything's ok. } timeout { timeout { sleep 10; } 3 or die "Inner timeout"; } 2 or die "Outter timeout"; print "Everything's ok.\n";

And the output is:

Outter timeout at ./testtimeout line 33.

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.


In reply to (bbfu) (nested-timeout fix) Re: Perl syntax for enforcing time limits by bbfu
in thread Perl syntax for enforcing time limits by ncw

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.