Hooray :D

Taking it up a notch here is my thoughts

#!/usr/bin/perl -- use strict; use warnings; { my $sentinel = expireAfter( 1, 9 ); print "begin ", scalar(gmtime),"\n"; while( $sentinel->() ){ print "loop ", scalar(gmtime),"\n"; } print "end ", scalar(gmtime),"\n"; } { my $sentinel = expireAfter( 1, 5 ); print "begin ", scalar(gmtime),"\n"; while( $sentinel->() ){ print "loop ", scalar(gmtime),"\n"; } print "end ", scalar(gmtime),"\n"; } #~ use constant DEBUG => 1 || !!$ENV{PERL_MYAPP_DEBUG}; use constant DEBUG => 0 || !!$ENV{PERL_MYAPP_DEBUG}; =head2 C<<< checkEveryExpireAfter( $every, $after ) >>> check $every seconds, if it hasn't been $every seconds yet, wait until + it has stop checking $after seconds have elapsed checkExpire( $every, $after ) intervalExpire( $every, $after ) afterExpire( $every, $expire ) everyUntil( $second, $expire ) breatheUntil( $second, $expire ) =cut sub expireAfter { my( $ten, $after ) = @_; my $lasttime = time; my $expire = $lasttime + $after; DEBUG and warn "## eA ## ten($ten)after($after)lasttime($lasttime) +expire($expire)"; return sub { my $newtime = time; my $diff = $newtime - $lasttime; if( $expire < $newtime ){ DEBUG and warn "## eA ## expired ( $expire < $newtime )"; return !!0; } elsif( $diff < $ten ){ DEBUG and warn "## eA ## sleep ( $diff < $ten )"; sleep abs( $diff - $ten ); } $lasttime = time; if( $expire < $lasttime ){ DEBUG and warn "## eA ## expired ( $expire > $lasttime )"; return !!0; } else { DEBUG and warn "## eA ## loop ( $expire > $lasttime )"; return !!1; } }; } sub expireAfterGood { my( $ten, $after ) = @_; my $lasttime = time; my $expire = $lasttime + $after; return sub { my $newtime = time; my $diff = $newtime - $lasttime; if( $expire < $newtime ){ warn "( $expire < $newtime )"; return !!0; } elsif( $diff < $ten ){ warn "expired ( $diff < $ten )"; sleep abs( $diff - $ten ); } $lasttime = time; if( $expire < $lasttime ){ warn "expired ( $expire > $lasttime )"; return !!0; } else { return !!1; } }; } sub expireAfterBuggy { my( $ten, $after ) = @_; my $lasttime = time; my $expire = $lasttime + $after; return sub { my $newtime = time; my $diff = $newtime - $lasttime; if( $expire > $lasttime ){ warn "( $expire > $lasttime )"; return !!0; } elsif( $diff < $ten ){ warn "expired ( $diff < $ten )"; sleep abs( $diff - $ten ); } $lasttime = time; if( $expire > $lasttime ){ warn "expired ( $expire > $lasttime )"; return !!0; } else { return !!1; } }; }

I started with expireAfterBuggy,

tweaked it until it did the right thing, and then called it expireAfterGood,

then improved the diagnostics, added documentation, tried to come up with a better name (this part needs work )

It is also possible to write it so that this does the right thing  while( nameHere( 'timername', 10, 3600 ) ){ ... } but it could "leak" memory if not used in a loop like that

Important thing, instead of  while( ( sleepy $time) and $time + 300 < $expiry )

have  while( oneThing($time, $expiry, 300) )

oneThing can have a sleepy and whatever else you need to make oneThing easier to understand ;) but since they both share $time and its simple scalar, stick it together under oneName


In reply to Re^5: while loop acting up, though I'm not sure how by Anonymous Monk
in thread while loop acting up, though I'm not sure how by msh210

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.