periodic#

Call the callback at wall-clock times: once at a given time, at every multiple of an interval, or whenever a sub of yours says.

Also available as EV::periodic_ns.

Synopsis#

my $at     = EV::periodic $epoch_seconds, 0, 0, sub { ... };   # once
my $hourly = EV::periodic 0, 3600, 0, sub { ... };             # each full hour
my $custom = EV::periodic 0, 0, \&next_time, sub { ... };      # you decide

What you get back#

An EV::Periodic watcher. The callback gets the watcher and EV::PERIODIC.

The three modes#

  • $interval and $reschedule_cb 0: fire once when the system clock reaches $at, however the clock is set meanwhile.

  • $interval above 0: fire at every $at + N * $interval, the next such time first (which may be before $at) - every full hour UTC above, without drift.

  • $reschedule_cb a sub: it is called with the watcher and the current time and returns the next time to fire, at or after that time. It must not stop or change any periodic watcher or call into the loop; to stop, return 1e30 and stop the watcher afterwards.

Examples#

Every midnight, local time:

use Time::Local ();
my $daily = EV::periodic 0, 0, sub {
    my (undef, $now) = @_;
    my (undef, undef, undef, $d, $m, $y) = localtime $now;
    Time::Local::timelocal_nocheck(0, 0, 0, $d + 1, $m, $y);
}, sub { print "midnight\n" };

Differences from upstream#

reschedule_cb keeps its sub and the watcher alive; see EV::Periodic. Covered by t/81-xs-native/EV/300-periodic-reschedule-cb.t.

See also#

  • EV::timer - relative timeouts

  • EV::Periodic - read and change a periodic watcher