I have the following code to print a "." each second to simulate a progress bar.
$num = 15;
while($num--){
sleep(1);
print ".";
}
The problem I'm having now is the "." character is not printed after each loop. Instead, all 15 "." are printed at once after the loop exits. However if I print ".\n", it works fine. But the "." will be printed on a new line every time which is not what I want.
It seems quite weird and could not figure out why. Could anyone provide some help? Thank you.
Regards, Allen
That's probably because I/O is being buffered. Try disabling buffering (autoflush):
$| = 1;
before your loop.
For a more complete explanation, refer to How do I flush/unbuffer an output filehandle? Why must I do this?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With