469,342 Members | 6,453 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

How to Autoflush STDOUT

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.
Feb 17 '07 #1
2 100426
miller
1,089 Expert 1GB
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

$|

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?
Feb 17 '07 #2
KevinADC
4,059 Expert 2GB
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.  
Feb 17 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Gianluca Trombetta | last post: by
reply views Thread by Herman Chan | last post: by
9 posts views Thread by Santtu Nyrhinen | last post: by
5 posts views Thread by =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post: by
5 posts views Thread by Joakim Hove | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.