console print issue

B

Bigus

This is driving me nuts and I've had it working in different scripts
before.. here's the code:

for($i=0;$i<5;$i++){
sleep 2;
print ">";
}

that should cause an angle bracket to be printed on the commandline every 2
seconds, but instead all 5 are printed at the same time when it's finished
the loop. Can anyone shed any light on why?

Bigus
 
E

Eugene Mikheyev

that should cause an angle bracket to be printed on the commandline every
2
seconds, but instead all 5 are printed at the same time when it's finished
the loop. Can anyone shed any light on why?

Sure, just turn on the autoflash.
$| = 1;
 
B

Bigus

Eugene Mikheyev said:
every

Sure, just turn on the autoflash.
$| = 1;

that's odd, because I've never used $| before and in some of my previous
scripts where I've looped through a folder of log files I've included a
progress-bar style thing, like:

where the first row of brackets is the number of log files and on the second
row a bracket is printed after each file has been processed.. and it's
worked fine. I guess something else must have forced a flush of teh print
statements in those cases.

Anyway, thanks :)
 
P

Paul Lalli

that's odd, because I've never used $| before and in some of my previous
scripts where I've looped through a folder of log files I've included a
progress-bar style thing, like:


where the first row of brackets is the number of log files and on the second
row a bracket is printed after each file has been processed.. and it's
worked fine. I guess something else must have forced a flush of teh print
statements in those cases.

Anyway, thanks :)

STDOUT defaults to line-buffering (when set to the terminal), files
default to block-buffering. This could account for the differences you
see. Also, printing a newline character flushes the buffer as well.

perldoc -q flush
and
perldoc perlop (search for $!)

for more info.

Paul Lalli
 
J

J. Romano

Eugene Mikheyev said:
Sure, just turn on the autoflash.
$| = 1;


If you prefer not to use the autoflush feature, but rather be able
to flush manually, you can always require the "flush.pl" module and
call "flush(*STDOUT)" whenever you need to, like in this example:

#!/usr/bin/perl -w
use strict;
require "flush.pl";

for (1 .. 5)
{
sleep 2;
print ">";
flush(*STDOUT);
}
__END__


-- Jean-Luc
 
J

John W. Krahn

Bigus said:
that's odd, because I've never used $| before and in some of my previous
scripts where I've looped through a folder of log files I've included a
progress-bar style thing, like:


where the first row of brackets is the number of log files and on the second
row a bracket is printed after each file has been processed.. and it's
worked fine. I guess something else must have forced a flush of teh print
statements in those cases.

You could have been printing to STDERR which isn't buffered.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top