GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Time::Out(3) User Contributed Perl Documentation Time::Out(3)

Time::Out - Easily timeout long running operations

  use Time::Out qw( timeout );
  timeout $timeout => sub {
    # your operation is implemented here and will be interrupted
    # if it runs for more than $timeout seconds
  };
  if ( $@ ) {
    # operation timed-out
  }

The "Time::Out" module provides an easy interface to alarm(2) based timeouts. Nested timeouts are supported. The module exports the timeout() function by default. The function returns whatever the code placed inside the subroutine reference returns:

  use Time::Out qw( timeout );
  my $result = timeout 5 => sub {
    return 7;
  };
  # $result == 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::HiRes qw();
  use Time::Out   qw( timeout );
  timeout 3.1416 => sub {
    # ...
  };

alarm(2) doesn't interrupt blocking I/O on MSWin32, so timeout() won't do that either.
@_
One drawback to using timeout() is that it masks @_ in the affected code. This happens because the affected code is actually wrapped inside another subroutine that provides it's own @_. You can get around this by specifically passing your @_ (or whatever you want for that matter) to timeout() as such:

  use Time::Out qw( timeout );
  sub foo {
    timeout 5, @_ => sub {
      @_;
    };
  }
  my @result = foo( 42, "Hello, World!" );
  # @result == ( 42, "Hello, World!" );
    
If the affected code has its own exception handling using Try::Tiny for example, the catch block has to be amended in a way so that it will rethrow an exception, if it refers to a timeout:

  use Scalar::Util qw( blessed  );
  use Time::Out    qw( timeout );
  use Try::Tiny    qw( catch try );
  timeout 5, sub {
    try {
      select( undef, undef, undef, 7 );
      die "bad\n";
    } catch {
      # rethrow exception, if it refers to a timeout
      die $_ if defined blessed $_ and $_->isa( 'Time::Out::Exception' );
      # handle all other exceptions
    }
  };
    

alarm(2), Sys::AlarmCall

Sven Willenbuecher, <sven.willenbuecher@gmx.de>

Patrick LeBoutillier, <patl@cpan.org>

This software is copyright (c) 2005-2008 Patrick LeBoutillier, 2023 by Sven Willenbuecher.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

2024-10-16 perl v5.40.2

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.