sandy105 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, i have been researching into some issues with STDOUT buffering .how many bytes does perl exactly buffer in STDOUT before writing it. Also is the behaviour any different for STDOUT and any buffered filehandle ?

p.s. i already have read the article suffering from buffering and use STDOUT autoflush in my newer scripts.

  • Comment on how many chars/bytes does STDOUT buffer

Replies are listed 'Best First'.
Re: how many chars/bytes does STDOUT buffer (test)
by tye (Sage) on Apr 02, 2015 at 13:35 UTC
    $ perl -MTime::HiRes=sleep -e'while(1){print ".";sleep 0.001}' | perl + -le'print sysread(STDIN,$rec,1024*1024)' 4096

    (no, the sleep isn't strictly necessary, but it makes the buffering effect more obvious.)

    - tye        

      I'm getting 8192. Perl 5.14.4 on Cygwin 64.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Perl's IO buffer size was changed from 4096 pre-5.14 to 8192 post 5.14. It's also configurable at build time.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

        Yeah, the point was the test, not the sample number produced. Since "line-buffer STDOUT to a terminal but full-buffered to a file or pipe" is the pattern for almost all programs, not just Perl, it is likely that the buffering is not done by Perl and so the size depends on your OS and/or C library version. Though, Perl has several choices of I/O layer implementation and one or more of them might re-implement that standard I/O buffer behavior but might choose different default sizes (though, I recall there being ways to specify buffering and the standard http://perl.plover.com/FAQs/Buffering.html documents such and much more).

        - tye        

Re: how many chars/bytes does STDOUT buffer
by MidLifeXis (Monsignor) on Apr 02, 2015 at 13:40 UTC

    IIRC, it is system dependent, but is usually some multiple of an IO Block Size.

    --MidLifeXis

Re: how many chars/bytes does STDOUT buffer
by sandy105 (Scribe) on Apr 03, 2015 at 06:29 UTC

    thanks for pointing out that its system dependant .

      It's not. See BrowerUk's answer.
      Yes, Perl does have its own buffer, in addition to whatever buffering the operating system may or may not do. The safest thing to do is to assume that the size of the buffer is unpredictable. If you need to explicitly flush it, do so, but do not build-in any assumptions as to how large it is.

        in addition to whatever buffering the operating system may or may not do.

        The OS may delay writing it to disk a little after receiving it, but it will appear to be on the disk to anyone trying to read the file immediately after Perl sends it to the OS.

        That means that if you you use flush or autoflush to send the data to the OS, it will be immediately available to everyone, though the data could be lost if a power outage occurs shortly after.