Instead of flushing the buffer can't we disable the buffering itself ?

I don't think there is much difference between flushing the buffer with every print or write and disabling the buffering, at least from an external perspective (i.e. without looking at the code that implements the functions). But you may mean something different than what I understand.

You may find PerlIO interesting. You can open a file with the 'unix' layer specified, in which case it is unbuffered, at least on my linux system.

use strict; use warnings; open(my $fh, '>:unix', "test.txt") or die "test.txt: $!"; foreach my $i (1..20) { print $fh "iteration $i\n"; sleep(5); } close($fh);

Perhaps this satisfies your definition of "disable the buffering".

You might also be interested to investigate the setbuf and setvbuf methods of IO::Handle.

Another option is to use syswrite which "bypasses buffered IO".

update: If you are interested in manipulating the I/O stack on a filehandle that is already open, such as STDOUT, you can use binmode (at least you can on linux, I don't know if this would work on all or any other platforms):

use strict; use warnings; use PerlIO; print join(', ', PerlIO::get_layers(*STDOUT)) . "\n"; binmode(*STDOUT, ":pop"); print join(', ', PerlIO::get_layers(*STDOUT)) . "\n"; __END__ unix, perlio unix

After this change, output to STDOUT is unbuffered even if STDOUT is redirected to a file or pipe.


In reply to Re: How to disable buffering ? by ig
in thread How to disable buffering ? by vinoth.ree

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.