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

What's going on, please, and why?

Rewrite the code so it tells you what is going on, its Basic debugging checklist

Basically start with something like this and run with it

use strict; use warnings; my $expiry = time + 3600; my $time; my $previoustime; #~ while ( 1 ) { for(1..10){ $time = time; my $and = $time + 300 < $expiry; my $or = $time - $previoustime >= 10; $or or my $orr = sleep (10 + $previoustime - $time); dd( [ time => $time], [ expiry => $expiry,], [ and => $and ], [ or + => $or, ], [ orr => $orr ] ); $previoustime = $time; warn $time; } __END__

Replies are listed 'Best First'.
Re^2: while loop acting up, though I'm not sure how
by msh210 (Monk) on Nov 17, 2015 at 00:11 UTC

    Thanks for your reply. There's no dd function in my version of Perl, but I used

    print "@$_" for @{[[ time => $time], [ expiry => $expiry,], [ and => $and ], [ or => $or, ], [ orr => $orr ] ]};

    instead and the output was singularly uninformative. I don't know what you were trying to point out in those results; perhaps you can clarify. (Or maybe dd would have been more informative?)

    However, I'll try other things from that debugging-advice page. Thanks for the link.

      Data::Dump brings in dd, sorry about that :)

      . I don't know what you were trying to point out in those results; perhaps you can clarify.

      Huh?

      If you want to figure out what is going on, you start by gathering information -- the beginning

      Then you either see your problem (wrong value, wrong condition, wrong assumption...) or look for more/different data or you throw away the whole thing and start over

      I'm not sure where your confusion lies so I can't clarify

      Maybe its as simple as

      while( tenSecondsHasPassed() ){ doStuff(); } sub tenSecondsHasPassed { my $newtime = time; my $diff = $newtime - $lasttime; ... ## if expired "end loop" ## if not ten seconds, sleep to make it ten seconds ## if expired "end loop" ## otherwise continue looping }

        Thank you again. I'm still not sure what was wrong with my original code, but using a subroutine has helped; thus:

        use strict; use warnings; my $expiry = time + 3600; my $time = time; sub sleepy { my $oldtime = pop; my $newtime = time; my $diff = $newtime - $oldtime; sleep 10 - $diff if $diff < 10; return 1; } while ( (sleepy $time) and $time + 300 < $expiry ) { $time = time; warn $time; }