Perl TimeOut


You have a script which runs too long and you want to time it out, after a given number
of seconds.



Useful bit of code to time-out a section of your Perl script, via the alarm function.

See the example tab.




eval {

local %SIG;
sub{ die “timeout reached, after 20 seconds\n”; };
alarm 20;
print “sleeping for 60 secondsn”;
sleep 60; # This is where to put your code, between the alarms
alarm 0;

alarm 0;

if($@) { print “Error: $@\n”; }





[tags]Perl timeout, Perl Coding School[/tags]


If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *