3

This sounds crazy but nothing shows up on the screen when I use the line printf(".") until I print something else. When I print out other info, from a simple line return to actual data all the backed up dots print out. If I use printf(".\n") then it works fine.

Am doing this on an Ubuntu system if that makes a difference.

Thanks

Daniel Kaplan
  • 701
  • 7
  • 19

2 Answers2

5

This is a buffering issue. Try setting autoflush: $| = 1;

{
    $| = 1;
    print ".";
}
sleep 1 for 1..5;

STDOUT is line-buffered, so print ".\n" works fine

More information: Suffering from buffering?

Another useful answer: Can you force-flush output in Perl?

Borodin
  • 126,100
  • 9
  • 70
  • 144
k-mx
  • 667
  • 6
  • 17
3

Like other have said, it is a buffering issue. In recent enough versions of Perl you can do:

 use IO::Handle
 STDOUT->autoflush()

I think this is cleaner than using the $| variable, even if it will work in the same way.

The use IO::Handle is needed to attach the autoflush method to any future handle. And autoflush has its quirks in the sense that without passing a value it assumes 1 as written in the perlvar man page:

The methods each take an optional EXPR, which, if supplied, specifies the new value for the "IO::Handle" attribute in question. If not supplied, most methods do nothing to the current value--except for "autoflush()", which will assume a 1 for you, just to be different.

Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54