<?xml version="1.0" encoding="windows-1252"?>
<node id="1007600" title="Re^3: Need Help With Alarm And Eval" created="2012-12-06 10:08:09" updated="2012-12-06 10:08:09">
<type id="11">
note</type>
<author id="747201">
afoken</author>
<data>
<field name="doctext">
&lt;blockquote&gt;removed local and every thing worked perfectly&lt;/blockquote&gt;
&lt;p&gt;No, it doesn't. The &lt;c&gt;On&lt;/c&gt; function sets the signal handler globally, for ever, even after &lt;c&gt;eval&lt;/c&gt; has finished. You want &lt;c&gt;local $SIG{'ALRM'}&lt;/c&gt; inside that &lt;c&gt;eval&lt;/c&gt;.&lt;/p&gt;
&lt;p&gt;The extra subs don't make your code any clearer. I think you should keep [doc://alarm] and &lt;c&gt;local $SIG{'ALRM'}&lt;/c&gt; inside the &lt;c&gt;eval&lt;/c&gt;.&lt;/p&gt;
&lt;p&gt;You could also consider writing a generic timeout function like this:&lt;/p&gt;
&lt;c&gt;
# untested

sub withTimeout
{
    my ($timeout,$worker,$timeouthandler)=@_;
    eval {
        local $SIG{'ALRM'}=sub { die "TIMEOUT" };
        alarm($timeout);
        $worker-&gt;();
        alarm(0);
    };
    if ($@ and $@=~/TIMEOUT/) {
        $errorhandler-&gt;();
    }
}
&lt;/c&gt;
&lt;p&gt;Alexander&lt;/p&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-747201"&gt;
--&lt;br&gt;
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1007553</field>
<field name="parent_node">
1007559</field>
</data>
</node>
