Yes, I think you want something like (warning, untested:)
while ($looping) { # mark the time $t = time(); # do whatever you want here execute_my_periodic_operation_here(); # wait until the end of 60 seconds # but don't hammer the cpu .. the sleep keeps # from calling time() way too often while(time() < $t+60) { sleep(1); } }
* note that the above example is not set to wait for even 60 second boundaries of t. I'm assuming you don't care. If clean minute boundaries are important, this needs to be implemented differently.
Another non-wheel-reinventing option might be to look at POE. POE is fairly cool, but it tends to not fit in with normal programming idioms, so it may change the architecture of your program somewhat.
Other monks may be able to suggest other CPAN modules that would be cleaner and more portable than the ALARM (or my version) and less heavyweight than POE. I'd be interested to hear what those were.
Update: merlyn's code below is more efficient and his code sleeps to the next 60 second boundary... this is closer to what you want.
In reply to Re: cron-like timing within perl script?
by flyingmoose
in thread cron-like timing within perl script?
by n8ur
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |