473,226 Members | 1,620 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,226 software developers and data experts.

How to Autoflush STDOUT

On execution of
$val = 4;
for ($i=0;$i<=$val;$i++) {
print "$i\n";

the output is

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 101898
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:



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. }
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.


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:

1) How do I flush/unbuffer an output filehandle? Why must I do this?
Feb 17 '07 #2
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. }
Feb 17 '07 #3

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

Similar topics

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
by: Herman Chan | last post by:
Hi: I have the following script ========================================== #!D:/Perl/bin/Perl.exe -w use strict; use CGI ":standard";
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"...
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...
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...
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...
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...
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...
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...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.