The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Coro::Timer - simple timer package, independent of used event loops

SYNOPSIS

 use Coro::Timer qw(sleep timeout);
 # nothing exported by default

 sleep 10;

DESCRIPTION

This package implements a simple timer callback system which works independent of the event loop mechanism used. If no event mechanism is used, it is emulated. The Coro::Event module overwrites functions with versions better suited.

This module is not subclassable.

$flag = timeout $seconds;

This function will wake up the current coroutine after $seconds seconds and sets $flag to true (it is false initially). If $flag goes out of scope earlier nothing happens. This is used to implement the timed_down, timed_wait etc. primitives. It is used like this:

   sub timed_wait {
      my $timeout = Coro::Timer::timeout 60;

      while (condition false) {
         schedule; # wait until woken up or timeout
         return 0 if $timeout; # timed out
      }
      return 1; # condition satisfied
   }
sleep $seconds

This function works like the built-in sleep, except maybe more precise and, most important, without blocking other coroutines.

$timer = new Coro::Timer at/after => xxx, cb => \&yyy;

Create a new timer.

$timer->cancel

Cancel the timer (the callback will no longer be called). This method MUST be called to remove the timer from memory, otherwise it will never be freed!

AUTHOR

 Marc Lehmann <schmorp@schmorp.de>
 http://home.schmorp.de/