Re: Print question

From: Sherm Pendley (spamtrap_at_dot-app.org)
Date: 01/12/05


Date: Tue, 11 Jan 2005 21:38:10 -0500

jl_post@hotmail.com wrote:

> Normally, a print() statement in Perl won't print a newline
> unless you tell it to.

Quite correct, but that doesn't answer the question. The OP is asking how to
unbuffer output, so it appears immediately, rather than a line at a time.
To illustrate this, insert a sleep(2) between each of the prints in your
example:

> #!/usr/bin/perl
> use strict;
> use warnings;
> print "H";

sleep(2);

> print "i";

sleep(2);

> print "!";

sleep(2);

> print "\n";
> __END__

You'd think this would print one letter at a time, with a pause in between -
but that's not what happens. What usually happens is a six second pause,
and then "Hi!" printed all at once, because output is buffered. (I say
"usually" because it's an OS feature, not a Perl feature, so it may not
apply to everyone. In particular, I don't know if it's true on Windows.)

The FAQ I mentioned addresses the buffering question. In brief - the FAQ
gives much more detail - you set $| to a true value to disable buffering.

So if you add:
    $| == 1;

To your example, you get what the OP presumably wants from the sleep(2)
calls above - a two-second pause between each letter.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


Relevant Pages

  • Re: How to pause/resume playing from within my source filter?
    ... The graph manager cues the graph by pausing. ... Generally, filters except ... transition to pause to be complete, ... a "buffering" UI indicator). ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: How to stop Media Center from buffering live TV for 30 minutes
    ... >How do I stop Media Center from buffering for 30 minutes a live TV picture. ... >I do not want to rewind or pause the picture, so there is no need to buffer. ...
    (microsoft.public.windows.mediacenter)
  • Re: How to pause/resume playing from within my source filter?
    ... pause the graph and wait for the pause to complete. ... This means handling the GetState method correctly. ... Then if the filter is still paused and not ready, ... click play, displays buffering status, then starts playing after a bit) except ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: tail -f | less
    ... I need to display a logfile as it's being dumped, and give the user the ability to pause and scroll back. ... The data needs not be kept but can be dumped into a ring buffer. ... There's some buffering issues here, but they can be solved, with a bit of effort. ...
    (comp.os.linux.misc)