Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

sleep in while loop

by Anonymous Monk
on Aug 05, 2002 at 15:36 UTC ( [id://187703]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Help - I trying to use a sleep in a while loop and run I the program it just hangs:

#!/usr/bin/perl -w while(1){ something sleep(10); }

I know I'm doing something daft, what is it ??


Edited: ~Mon Aug 5 16:12:50 2002 (GMT) by footpad: Added <code> and other HTML formatting tags, per Consideration.

Replies are listed 'Best First'.
Re: sleep in while loop
by Abigail-II (Bishop) on Aug 05, 2002 at 15:44 UTC
    What do you mean by "it just hangs"?
    while (1) { ... do something ...; sleep 10 }
    isn't going to terminate, unless you exit the loop from the do something part.


      I don't want it to exit from the loop, when I say it just hangs I mean it doesn't execute any other code, it just seems to go straight to the sleep function and stay there. I want the prog to loop indefinitely until it is killed by the user. i.e: while (1) { if($cond = 1){ print "1"; } else{ print "2"; } sleep 10 } it doesn't do the conditional statements (just eg's) or any other statements in the prog, it just hangs on the cmd line: ./ Huh ?
        You may be getting bitten by buffering. Try this:
        $|++; # force auto-flush (see perldoc perlvar) while (1) { if ($cond == 1) { print "1\n"; } else { print "2\n"; } sleep 10; }

Re: sleep in while loop
by krisahoch (Deacon) on Aug 05, 2002 at 16:06 UTC

    This is almost scary. Abigail-II said that this loop will never end, and he is quite right. This almost looks like the dreaded (( Infinite Loop ))

    while(1) { something... sleep(10); }

    You are basically asking "Is 1 actually 1?". The answer of course is Yes, so do something and sleep for ten seconds. There are a couple of sure fire ways you can get out of this;
    1) return if you are in a sub routine
    2) exit for anything else.

    One other way I can think of is to make 1 no longer equal to 1, but I don't think you'll have much luck. Maybe post what the 'do something' section of you code is doing. That will give us more to work on.

    Kristofer Hoch

    Note: Updated to accurately apply pronouns to the Abigail-II antecedant

          The Perl Monks are 'hard' to beat to the punch.

          To be "first" with the correct answer is a real accomplishment.

          Great group here!

          Heh, happens to all of us at sometime or another. Dont let it get you down.

          Yves / DeMerphq
          Writing a good benchmark isnt as easy as it might look.

    Re: sleep in while loop
    by arturo (Vicar) on Aug 05, 2002 at 16:11 UTC

      It's hard to know what's going on unless you've tell us more about what's going on in the ".. do something" part. How do you know your conditional test (which your additions mention) isn't getting executed at all instead of just failing?

      Try putting a print in there or something that is guaranteed to execute:

      my $entered; while(1) { print "Entered while loop\n" unless $entered++; # get input, read file, read socket, or whatever here # ... if ( $test ) { #... last; } sleep(10); }


      I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche

    Re: sleep in while loop
    by Anonymous Monk on Aug 05, 2002 at 16:26 UTC
      Thanks - buffering was biting my proverbial ***- by the way the code in the loop was not the actual code - sorry to confuse the issue

    Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: perlquestion [id://187703]
    Approved by footpad
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others taking refuge in the Monastery: (2)
    As of 2024-06-20 18:59 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.