kurtis has asked for the wisdom of the Perl Monks concerning the following question:

I've wrapped my main code within a timeout to keep it from running too long. However, I'd like to know exactly which line of my main code was being processed as the alarm was triggered. All I get back is the line of the $SIG{ALRM}, which I already know. I would like to know the line in main.pl that was being eval'ed immediately prior to the alarm going off. Is there any way to do this? Thanks.
local $SIG{ALRM} = sub { die "timeout" }; eval { alarm(12); require './main.pl'; &generate_page; alarm(0); }; if ($@) { if ( $@ =~ /timeout/ ) { croak "TIMEOUT: $@"; } else { alarm(0); # clear the still-pending alarm die "$@"; } }

Replies are listed 'Best First'.
Re: Finding the exact line in eval'ed code when alarm timed out
by GrandFather (Saint) on Oct 13, 2008 at 20:43 UTC

    Check out caller:

    local $SIG{ALRM} = sub { my @context = caller 0; die "timeout: in $context[1] at line $context[2]\n" };

    Perl reduces RSI - it saves typing
      That worked perfectly, thanks!