1Out(3) User Contributed Perl Documentation Out(3)
2
3
4
6 Time::Out - Easily timeout long running operations
7
9 use Time::Out qw(timeout) ;
10
11 timeout $nb_secs => sub {
12 # your code goes were and will be interrupted if it runs
13 # for more than $nb_secs seconds.
14 } ;
15 if ($@){
16 # operation timed-out
17 }
18
20 "Time::Out" provides an easy interface to alarm(2) based timeouts.
21 Nested timeouts are supported.
22
23 RETURN VALUE
24 'timeout' returns whatever the code placed inside the block returns:
25
26 use Time::Out qw(timeout) ;
27
28 my $rc = timeout 5 => sub {
29 return 7 ;
30 } ;
31 # $rc == 7
32
34 If "Time::Out" sees that "Time::HiRes" has been loaded, it will use
35 that 'alarm' function (if available) instead of the default one,
36 allowing float timeout values to be used effectively:
37
38 use Time::Out ;
39 use Time::HiRes ;
40
41 timeout 3.1416 => sub {
42 # ...
43 } ;
44
46 Blocking I/O on MSWin32
47 alarm(2) doesn't interrupt blocking I/O on MSWin32, so 'timeout'
48 won't do that either.
49
50 @_ One drawback to using 'timeout' is that it masks @_ in the affected
51 code. This happens because the affected code is actually wrapped
52 inside another subroutine that provides it's own @_. You can get
53 around this by specifically passing your @_ (or whatever you want
54 for that matter) to 'timeout' as such:
55
56 use Time::Out ;
57
58 sub test {
59 timeout 5, @_ => sub {
60 print "$_[0]\n" ;
61 } ;
62 }
63
64 test("hello") ; # will print "hello\n" ;
65
67 eval, closures, alarm(2), Sys::AlarmCall
68
70 Patrick LeBoutillier, <patl@cpan.org>
71
73 Copyright 2005-2008 by Patrick LeBoutillier
74
75 This library is free software; you can redistribute it and/or modify it
76 under the same terms as Perl itself.
77
78
79
80perl v5.28.1 2009-05-04 Out(3)