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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.