Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Problem with sleep command in perl

by Anonymous Monk
on Aug 03, 2012 at 08:30 UTC ( #985187=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

When the below script is executed, sleep command is executed first and only after that the print statements are executed though they are present before the sleep command given . what might be the reason for this strange behaviour ???? thanks in advance!!!
#! /usr/bin/perl -w print "First line of the script"; print "Second line of the script"; $time = 5; while($time > 0) { sleep (5); $time--; }

Comment on Problem with sleep command in perl
Download Code
Replies are listed 'Best First'.
Re: Problem with sleep command in perl
by choroba (Canon) on Aug 03, 2012 at 08:34 UTC
    Search for "buffering".
Re: Problem with sleep command in perl
by bulk88 (Priest) on Aug 03, 2012 at 15:50 UTC
    You are using buffered stdio. Only when the buffer has a newline will it print to console. Turn on autoflush http://perldoc.perl.org/perlvar.html#$| every print should cause a console print, but this also causes higher cpu and is less efficient.

    edit for mistake pointed out by another monk

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985187]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2015-07-28 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (253 votes), past polls