Out(3pm) | User Contributed Perl Documentation | Out(3pm) |
Time::Out - Easily timeout long running operations
use Time::Out qw(timeout) ; timeout $nb_secs => sub { # your code goes were and will be interrupted if it runs # for more than $nb_secs seconds. } ; if ($@){ # operation timed-out }
"Time::Out" provides an easy interface to alarm(2) based timeouts. Nested timeouts are supported.
'timeout' returns whatever the code placed inside the block returns:
use Time::Out qw(timeout) ; my $rc = timeout 5 => sub { return 7 ; } ; # $rc == 7
If "Time::Out" sees that "Time::HiRes" has been loaded, it will use that 'alarm' function (if available) instead of the default one, allowing float timeout values to be used effectively:
use Time::Out ; use Time::HiRes ; timeout 3.1416 => sub { # ... } ;
use Time::Out ; sub test { timeout 5, @_ => sub { print "$_[0]\n" ; } ; } test("hello") ; # will print "hello\n" ;
eval, closures, alarm(2), Sys::AlarmCall
Patrick LeBoutillier, <patl@cpan.org>
Copyright 2005-2008 by Patrick LeBoutillier
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2022-10-22 | perl v5.34.0 |