The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Time::Out - Easily timeout long running operations

SYNOPSIS

  use Time::Out ;

  timeout $nb_secs => affects {
    # your code goes were and will be interrupted if it runs
    # for more than $nb_secs seconds.
  } ;
  if ($@){
    # operation timed-out
  }

DESCRIPTION

Time::Out provides an easy interface to alarm(2) based timeouts.

EXPORT

Time::Out exports 2 symbols, 'timeout' and 'affects'. However, these should only be used according to the syntax shown above.

RETURN VALUE

'timeout' returns whatever the code placed inside the block returns:

  use Time::Out ;

  my $rc = timeout 5 => affects {
        return 7 ;
  } ;
  # $rc == 7

BUGS

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 ;

  sub test {
    timeout 5, @_ => affects {
      print "$_[0]\n" ;
    } ;
  }

  test("hello") ; # will print "hello\n" ;

SEE ALSO

eval, alarm(2)

AUTHOR

Patrick LeBoutillier, <patl@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2005 by Patrick LeBoutillier

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