How can I tell Perl to run some code every 20 seconds?

Viewed 22859

How can I tell Perl to run some code every 20 seconds?

8 Answers

It is better to use some event library. There are a couple of choices:

IO::Async::Timer::Periodic

use IO::Async::Timer::Periodic;

use IO::Async::Loop;
my $loop = IO::Async::Loop->new;

my $timer = IO::Async::Timer::Periodic->new(
   interval => 60,

   on_tick => sub {
      print "You've had a minute\n";
   },
);

$timer->start;

$loop->add( $timer );

$loop->run;

AnyEvent::DateTime::Cron

AnyEvent::DateTime::Cron->new()
    ->add(
        '* * * * *'   => sub { warn "Every minute"},
        '*/2 * * * *' => sub { warn "Every second minute"},
      )
    ->start
    ->recv

EV::timer

etc.

Related