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

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.


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

    Perl does not support truly unbuffered output (except insofar as you can
    "syswrite(OUT, $char, 1)"), although it does support is "command
    buffering", in which a physical write is performed after every output

    The C standard I/O library (stdio) normally buffers characters sent to
    devices so that there isn't a system call for each byte. In most stdio
    implementations, the type of output buffering and the size of the buffer
    varies according to the type of device. Perl's print() and write()
    functions normally buffer output, while syswrite() bypasses buffering
    all together.

    If you want your output to be sent immediately when you execute print()
    or write() (for instance, for some network protocols), you must set the
    handle's autoflush flag. This flag is the Perl variable $| and when it
    is set to a true value, Perl will flush the handle's buffer after each
    print() or write(). Setting $| affects buffering only for the currently
    selected default file handle. You choose this handle with the one
    argument select() call (see "$|" in perlvar and "select" in perlfunc).

    Use select() to choose the desired handle, then set its per-filehandle

        $old_fh = select(OUTPUT_HANDLE);
        $| = 1;

    Some idioms can handle this in a single statement:

        select((select(OUTPUT_HANDLE), $| = 1)[0]);

        $| = 1, select $_ for select OUTPUT_HANDLE;

    Some modules offer object-oriented access to handles and their
    variables, although they may be overkill if this is the only thing you
    do with them. You can use IO::Handle:

        use IO::Handle;
        open(DEV, ">/dev/printer");   # but is this?

    or IO::Socket:

        use IO::Socket;               # this one is kinda a pipe?
            my $sock = IO::Socket::INET->new( 'www.example.com:80' ) ;



Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.


    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

Site Timeline