FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this?

Discussion in 'Perl Misc' started by PerlFAQ Server, Apr 2, 2011.

  1. This is an excerpt from the latest version perlfaq5.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .

    --------------------------------------------------------------------

    5.1: How do I flush/unbuffer an output filehandle? Why must I do this?

    (contributed by brian d foy)

    You might like to read Mark Jason Dominus's "Suffering From Buffering"
    at http://perl.plover.com/FAQs/Buffering.html .

    Perl normally buffers output so it doesn't make a system call for every
    bit of output. By saving up output, it makes fewer expensive system
    calls. For instance, in this little bit of code, you want to print a dot
    to the screen for every line you process to watch the progress of your
    program. Instead of seeing a dot for every line, Perl buffers the output
    and you have a long wait before you see a row of 50 dots all at once:

    # long wait, then row of dots all at once
    while( <> ) {
    print ".";
    print "\n" unless ++$count % 50;

    #... expensive line processing operations
    }

    To get around this, you have to unbuffer the output filehandle, in this
    case, "STDOUT". You can set the special variable $| to a true value
    (mnemonic: making your filehandles "piping hot"):

    $|++;

    # dot shown immediately
    while( <> ) {
    print ".";
    print "\n" unless ++$count % 50;

    #... expensive line processing operations
    }

    The $| is one of the per-filehandle special variables, so each
    filehandle has its own copy of its value. If you want to merge standard
    output and standard error for instance, you have to unbuffer each
    (although STDERR might be unbuffered by default):

    {
    my $previous_default = select(STDOUT); # save previous default
    $|++; # autoflush STDOUT
    select(STDERR);
    $|++; # autoflush STDERR, to be sure
    select($previous_default); # restore previous default
    }

    # now should alternate . and +
    while( 1 )
    {
    sleep 1;
    print STDOUT ".";
    print STDERR "+";
    print STDOUT "\n" unless ++$count % 25;
    }

    Besides the $| special variable, you can use "binmode" to give your
    filehandle a ":unix" layer, which is unbuffered:

    binmode( STDOUT, ":unix" );

    while( 1 ) {
    sleep 1;
    print ".";
    print "\n" unless ++$count % 50;
    }

    For more information on output layers, see the entries for "binmode" and
    "open" in perlfunc, and the "PerlIO" module documentation.

    If you are using "IO::Handle" or one of its subclasses, you can call the
    "autoflush" method to change the settings of the filehandle:

    use IO::Handle;
    open my( $io_fh ), ">", "output.txt";
    $io_fh->autoflush(1);

    The "IO::Handle" objects also have a "flush" method. You can flush the
    buffer any time you want without auto-buffering

    $io_fh->flush;



    --------------------------------------------------------------------

    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    perlfaq.pod.
    PerlFAQ Server, Apr 2, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krutibas  Biswal

    unbuffer script using pexpect

    Krutibas Biswal, Jul 14, 2005, in forum: Python
    Replies:
    0
    Views:
    475
    Krutibas Biswal
    Jul 14, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,769
    Smokey Grindel
    Dec 2, 2006
  3. NeoGeoSNK
    Replies:
    25
    Views:
    909
    NeoGeoSNK
    Nov 24, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    126
    PerlFAQ Server
    Jan 9, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    185
    PerlFAQ Server
    Jan 12, 2011
Loading...

Share This Page