Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print text immediately without waiting for a newline in Perl?

I have a computationally expensive task in perl, and would like to inform the user that computation is ongoing by printing out a period after each portion of the computation is completed. Unfortunately, until I print a "\n", none of my periods are printed. How can I address this?

like image 994
shino Avatar asked Mar 12 '10 16:03

shino


1 Answers

You need to set autoflush for STDOUT. Example:

use IO::Handle;
STDOUT->autoflush(1);
foreach (1..20) {
  print '.';
  sleep(1);
}
like image 63
Alexandr Ciornii Avatar answered Sep 19 '22 00:09

Alexandr Ciornii