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

Can some one tell me how sleep is working. If i am giving sleep inside loop witout "\n" its not working.
This code is not working
while(1){ print "Test"; sleep(1); }

whereas this is working
while(1){ print "Test\n"; sleep(1); }
-Meridius-

Replies are listed 'Best First'.
Re: Sleep Not Sleeping when using without "\n"
by ikegami (Patriarch) on Mar 24, 2005 at 06:11 UTC
    STDOUT is buffered. "\n" forces the buffer to empty, and so does auto-flushing. Turn auto-flushing with $|=1, and the "\n" won't be necessary for the print to work immediately.
      Or just be very, very, patient :)
        From the Suffering from Buffering article, link in comment below, we get the line: "The blocks on your disk are probably about 8K bytes."

        Using that, and your original, non '\n', case, plus the sleep of 1 second, we get:

        8K = 8 * 1024 = 8192 bytes 'test' = 4 bytes 8192/4 = 2048 = # of times output will need to happen before you se +e anything due to buffering. 2048 * 1 second sleeps between each output = 2048 seconds 2048 / 60 seconds per minute = 34.1333 minutes to wait.
        -Scott
Re: Sleep Not Sleeping when using without "\n"
by BUU (Prior) on Mar 24, 2005 at 06:31 UTC