Connecting Tech Pros Worldwide Help | Site Map

How to Autoflush STDOUT

  #1  
Old February 17th, 2007, 12:48 PM
Newbie
 
Join Date: Feb 2007
Posts: 15
On execution of
#!/usr/bin/perl
$val = 4;
for ($i=0;$i<=$val;$i++) {
print "$i\n";
sleep(1);
}

the output is
0
1
2
3
4

However if I remove the newline character from the print statement, the output on my STDOUT is seen only after the entire for loop has been completed. I would assume the output to happen as soon as each for iteration is done.
i.e I get the output in one shot as follows
0 1 2 3 4
I assumed it would print 0... 4 after intervals of 1 second.
  #2  
Old February 17th, 2007, 05:40 PM
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830

re: How to Autoflush STDOUT


That's because you haven't turned autoflush on for that STDOUT pipe. Please read the following documentation which I'll excerpt here:

http://perldoc.perl.org/perlvar.html

Quote:
Originally Posted by perldoc
$|

If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0 (regardless of whether the channel is really buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after each write). STDOUT will typically be line buffered if output is to the terminal and block buffered otherwise. Setting this variable is useful primarily when you are outputting to a pipe or socket, such as when you are running a Perl program under rsh and want to see the output as it's happening. This has no effect on input buffering. See getc for that. (Mnemonic: when you want your pipes to be piping hot.)
Therefore to get your code to print out after each print statement, instead of each return character, simply add the following line:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. local $| = 1;
  4. my $val = 4;
  5. for (my $i=0; $i<=$val; $i++) {
  6.     print "$i ";
  7.     sleep(1);
  8. }
  9.  
Also, you'll notice that I added the line "use strict;" do your program and scoped your two variables. This is a very good habit to get in, and it will save you a lot of grief in the future so I recommend that you ALWAYS do this.

http://perldoc.perl.org/strict.html

Finally, if you want to know more about buffering from a file perspective, you read the faq on files. The first question speaks directly to your issue:

http://perldoc.perl.org/perlfaq5.html
1) How do I flush/unbuffer an output filehandle? Why must I do this?
  #3  
Old February 17th, 2007, 09:01 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

re: How to Autoflush STDOUT


just a bit more perlish way to code what miller already posted:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use diagnostics; # good for detailed explanations about any problems in code
  5. $| =1;
  6. my $val = 4;
  7. for (0..$val) {
  8.    print "$_ ";
  9.    sleep(1);
  10. }
  11.  
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Capturing stdout from a console app -- not working Oz answers 1 November 22nd, 2005 10:13 PM
Strange behavior of redirected stdout. Streams Broken? Oz answers 4 August 13th, 2005 07:15 AM
Capturing stdout from a console app -- not working Oz answers 1 July 23rd, 2005 06:45 AM
Autoflush in python cgi Gianluca Trombetta answers 1 July 18th, 2005 12:28 PM