How to count "reliably" 300 seconds time in perl?

perl, time

Solution

I think using the alarm function would make sense here.

{
  local $SIG{ALRM} = sub {
     warn "Ooops! timed out, exiting";
     exit(100); # give whatever exit code you want
  };

  ## setup alaram
  alarm( 5 * 60 );
  my $success = 0;
  until($success) {
    $success = try_something()
       or sleep 3;
  }

  ## deactivate alarm if successful
  alarm(0);
}

Problem

How can I make a 5 minutes timeout? My program is doing this: ``` # I need to try something every 3 seconds, for at most 5 minutes $maxtime = time() + (5 * 60); $success = 0; while (($success == 0) && (time() < $maxtime)) { $success = try_something(); sleep (3) if ($success == 0); } ``` The problem: this program runs just after boot. The embedded system that it runs has no rtc/clock battery. The clock starts at Jan/1/2000, then in the first minute it runs, it gets network and ntp sets the clock to the updated clock, making the loop exit before the 5 minutes timeout. Which is the right way to "count 5 minutes" inside a perl script, even if the system clock is changed by other external program?

Original source