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

I must be missing something here, but I was wondering how you would use a timer in perl. Take for example a scenario in which you needed to modify a variable after a certain time period. This is part of a larger program so the sleep function doesn't appear to be an option in my newbie eyes. Monks, some enlightenment please?

Replies are listed 'Best First'.
Re: timers in perl
by BrowserUk (Patriarch) on Jan 15, 2003 at 09:25 UTC

    If your not using Active State Perl, take a look at pelrfunc:alarm function.

    If you are using AS then you'll need to fork and use sleep in the child process and then the waitpid or similar methods to acheive this. A super search for "alarm Win32" should get you several hits where this has been discussed before.


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: timers in perl
by Abigail-II (Bishop) on Jan 15, 2003 at 09:40 UTC
    There are some ways of doing this. One is the use of SIGALRM, but you need to be quite careful when using signals. Another way is to use threads; one thread could sleep, and set the variable after the specified time. Or you could use some event driven programming technique, for instance based on POE, Tk or Event.

    Alternatively, you can tie the variable, and let the FETCH method check wether the allocated time has elapsed.

    Abigail

      Ahh the SIGALARM did it. I'll definetly look into those other methods though. Thanks a million, both of ya :)
Re: timers in perl
by vek (Prior) on Jan 15, 2003 at 12:57 UTC
    Might be overkill but you could always take a peek at Time::HiRes.

    -- vek --