narashima has asked for the wisdom of the Perl Monks concerning the following question:

Revered Monks,
My Active Perl script has the following structure:

while(some condition)
{
some operations;
sleep 300;
}

Now I need the script to perform a certain operation at exactly 4:00pm everyday and then exit.
Is there some timer mechanism in perl which lets me define a timer and also lets me define a timer expiry handler which gets executed when the timer expires?
Is there a differentway of doing this other than using timers?

Greatly appreciate any help here.
Thanks

Replies are listed 'Best First'.
Re: Timers and timer expire handlers
by Corion (Patriarch) on Mar 27, 2006 at 20:38 UTC

    You want to schedule your script via the cron daemon. It can launch your script at predefined times conveniently. The cron manpage has information on how to do this.

    If cron is not available on your system, there is Schedule::Cron, with which you can write your own small implementation of cron to launch your program at specific times. For the expiry timer, set up an alarm handler for $SIG{ALRM}, and prime it with alarm(300) or however many seconds you want to give your program:

    eval { local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required alarm $timeout; $nread = sysread SOCKET, $buffer, $size; alarm 0; }; if ($@) { die unless $@ eq "alarm\n"; # propagate unexpected errors # timed out } else { # didn't }
      Corion, thanks for the reply. However I am running this on Active state perl on Windows NT. Unfortunately my version of Activestate does not support Schedule::cron and cron itself is not available.
      Access to Windows scheduler is disabled at group level.
      Please help.

      Thanks,
      Bhavin
Re: Timers and timer expire handlers
by mda2 (Hermit) on Mar 28, 2006 at 00:51 UTC
    You can make a cron-like process using a relative sleep...
    Start your script with wperl.exe, running without console (check Daemonize process).
    I ran a similar code, started with wperl.exe, at W2K without problems (except reboot or admin changes) ...

    --
    Marco Antonio
    Rio-PM