473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 101930
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

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

Similar topics

1
by: Gianluca Trombetta | last post by:
How can i do the autoflush effect in a python cgi? In perl i know how do that but not in python... Can anyone help me? Thanks in advance Gianluca Trombetta
0
by: Herman Chan | last post by:
Hi: I have the following script ========================================== #!D:/Perl/bin/Perl.exe -w use strict; use CGI ":standard";
0
by: dede | last post by:
Dear community, having written a working example for using Semphores on a windows client, I created a little "server-application" that does the following: A Server continously "listens/tails"...
0
by: jabailo | last post by:
I was having some performance problems with a web service that is being called many times asynchronously. I was using a trace listener defined in the web.config and had autoflush=true. When I...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
9
by: Santtu Nyrhinen | last post by:
Hi, Let say that I have a function like void writeHello() { printf("Hello"); } Now I need to make an automated test fot that function. The test function returns 1 for successful and 0 for...
5
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post by:
I wanna print the log to both the screen and file, so I simulatered a 'tee' class Tee(file): def __init__(self, name, mode): file.__init__(self, name, mode) self.stdout = sys.stdout...
5
by: Joakim Hove | last post by:
Hello, I have written a program in C; this programs uses an external proprietary library. When calling a certain function in the external library, the particular function writes a message to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.