1Time::Out(3) User Contributed Perl Documentation Time::Out(3)
2
3
4
6 Time::Out - Easily timeout long running operations
7
9 use Time::Out qw( timeout );
10
11 timeout $timeout => sub {
12 # your operation is implemented here and will be interrupted
13 # if it runs for more than $timeout seconds
14 };
15 if ( $@ ) {
16 # operation timed-out
17 }
18
20 The "Time::Out" module provides an easy interface to alarm(2) based
21 timeouts. Nested timeouts are supported. The module exports the
22 timeout() function by default. The function returns whatever the code
23 placed inside the subroutine reference returns:
24
25 use Time::Out qw( timeout );
26
27 my $result = timeout 5 => sub {
28 return 7;
29 };
30 # $result == 7
31
32 If "Time::Out" sees that "Time::HiRes" has been loaded, it will use
33 that alarm() function (if available) instead of the default one,
34 allowing float timeout values to be used effectively:
35
36 use Time::HiRes qw();
37 use Time::Out qw( timeout );
38
39 timeout 3.1416 => sub {
40 # ...
41 };
42
44 Blocking I/O on MSWin32
45 alarm(2) doesn't interrupt blocking I/O on MSWin32, so timeout()
46 won't do that either.
47
48 @_
49 One drawback to using timeout() is that it masks @_ in the affected
50 code. This happens because the affected code is actually wrapped
51 inside another subroutine that provides it's own @_. You can get
52 around this by specifically passing your @_ (or whatever you want for
53 that matter) to timeout() as such:
54
55 use Time::Out qw( timeout );
56
57 sub foo {
58 timeout 5, @_ => sub {
59 @_;
60 };
61 }
62 my @result = foo( 42, "Hello, World!" );
63 # @result == ( 42, "Hello, World!" );
64
66 alarm(2), Sys::AlarmCall
67
69 Sven Willenbuecher, <sven.willenbuecher@gmx.de>
70
71 Patrick LeBoutillier, <patl@cpan.org>
72
74 This software is copyright (c) 2005-2008 Patrick LeBoutillier, 2023 by
75 Sven Willenbuecher.
76
77 This is free software; you can redistribute it and/or modify it under
78 the same terms as the Perl 5 programming language system itself.
79
80
81
82perl v5.36.1 2023-11-06 Time::Out(3)