Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: while loop acting up, though I'm not sure how

by Athanasius (Archbishop)
on Nov 17, 2015 at 06:28 UTC ( [id://1147884]=note: print w/replies, xml ) Need Help??


in reply to while loop acting up, though I'm not sure how

Hello msh210,

The logic of your while loop condition is too complicated. Rearranging and simplifying, I came up with this, which seems to do what you want:

#! perl use strict; use warnings; use constant { DELAY => 10, EXPIRY => 3600, MARGIN => 300, }; my $time = time; my $expiry = $time + EXPIRY; while ($time + MARGIN < $expiry) { my $previoustime = $time; warn $time; sleep DELAY + $previoustime - $time unless $time - $previoustime >= DELAY; $time = time; }

Output:

16:17 >perl 1453_SoPW.pl 1447741073 at 1453_SoPW.pl line 18. 1447741083 at 1453_SoPW.pl line 18. 1447741093 at 1453_SoPW.pl line 18. 1447741103 at 1453_SoPW.pl line 18. 1447741113 at 1453_SoPW.pl line 18. ...

A side note: sleep returns the number of seconds slept (as an integer). So the condition:

( $time - $previoustime >= 10 or sleep (10 + $previoustime - $time) )

could evaluate to zero — which is false — causing the loop to terminate prematurely. In my testing I don’t see this happening, but it’s still a logic error (a potential bug), and so a further reason to simplify the logic of the loop condition.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1147884]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found