ben hengst > Util-Timeout-0.01 > Util::Timeout



Annotate this POD

View/Report Bugs
Module Version: 0.01   Source  


  use Util::Timeout;
  timeout $seconds { ... } or do { ... };

  retry $times { ... } or do { ... };


Sys::SigAction::timeout_call sets a timer for $seconds, if your code block is still running when the timer trips then it is killed off. timeout then returns a false value thus you can chain with 'or' to allow for a clean syntaticaly correct syntax



  timeout 1 { sleep(2) } or do { $error = 'timed out' };

REMEMBER: these are lexical blocks (like eval) so any vars that you want to use else where will need to be scoped as such.

Also note, due to alarm not allowing for decimal numbers, all values are rounded up. Any value given for $seconds that is <= 0 will shortcut and your code block will not be executed and 0 returned.


  my $num = 3; 
  retry 5 { timeout 1 { sleep( $num-- ) } } or do { $error = 'timed out 5 times' };

retry will run your the code block, if the block returns true then we stop running and return '1'. If your code block returns false then it is run again, up to $times number of times (5 in the exampele), in this case rerun returns '0' allowing you to use 'or' like with timeout.

$times is expeceted to be an int, any decimal value will be rounded up. If $times is <= 1 then your code block will not be run and 0 will be returned;

syntax highlighting: