The STDOUT is a buffered handle. Buffered I/O is the typical on Unix systems - it saves the system time from having to start I/O, perform I/O, and stop I/O as often, by waiting for a certain amount of I/O to be "ready" before doing it. Imagine, for example, if your printer was warmed up, told to position the print head, write a single character, then told to reset the print head, every time you did a print LPR 'x' - a very bad thing. On a smaller scale, this applies also to disc I/O, network communications, &c. - there's almost always a "cost" to starting and stopping I/O.

However, when you're working with a "tty," it's pretty unlikely to be an actual TeleType(TM) these days. What I tend to do, is add a chunk like this up front:

use FileHandle; # Maybe should be File::IO ? if ( -t STDOUT ) { STDOUT->autoflush(1); } # OR: without FileHandle, save some RAM at the expense # of legibility: # braces keep "my" vars local here. { # Save the currently select:ed filehandle! my $saved_selected_fh = select STDOUT; if ( -t ) { ++$|; # turn on "autoflush" } }

N.B. that your "flush" code is not a flush (see  perldoc on  flush) -- it's actually toggling on the autoflush mode on STDOUT. That's a fancy way of saying that Perl will automatically  flush the STDOUT buffer - i.e. tell the OS to do the I/O right now, don't wait for more data - every time you do a  print or other write to it.

Since TTY or disc I/O is so fast, I'd recommend just sticking a  ++$| (turn on autoflush on the currently-selected output channel, which is STDOUT unless you've done a  select; the current channel is the one that  print LIST will print to, as opposed to  print FILEHANDLE LIST) at the beginning, rather than re-enabling autoflush for each line of data. It's not a big performance hit, but it is a bit of overkill.

If you really do want to manually control when the buffer is flushed (barring Unix getting all smart and surprising you by doing it first ... it can if it wants to ), just put a  flush STDOUT; in there.

Just for reference, on most unices the buffer for a tty is about 4Kbytes. If you changed a  print to produce, say, 4097 (4 * 1024 + 1) bytes of output, it would probably trigger a flush right off...


In reply to Re: STDOUT flush, Was: Simple requests using LWP::UserAgent by baku
in thread Simple requests using LWP::UserAgent by jlongino

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.