Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? PerlFAQ Server 03-07-2008
Posted by PerlFAQ Server on March 7, 2008, 9:03 pm
Please log in for more thread options
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?


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
command.

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 filehandle. 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
variables.

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

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 my( $printer ), ">", "/dev/printer"); # but is this?
$printer->autoflush(1);

or "IO::Socket" (which inherits from "IO::Handle"):

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

$sock->autoflush();

You can also flush an "IO::Handle" object without setting "autoflush".
Call the "flush" method to flush the buffer yourself:

use IO::Handle;
open my( $printer ), ">", "/dev/printer");
$printer->flush; # one time 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.

Posted by Ben Morrow on March 7, 2008, 11:00 pm
Please log in for more thread options

>
> 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
> command.
>
> 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.

Is it worth mentioning

binmode $HANDLE, ':unix';

somewhere here, which will make all prints equivalent to syswrites?

Ben


Posted by Peter J. Holzer on March 9, 2008, 6:07 am
Please log in for more thread options
>
>>
>> 5.1: How do I flush/unbuffer an output filehandle? Why must I do this?
>>
[...]
>
> Is it worth mentioning
>
> binmode $HANDLE, ':unix';
>
> somewhere here, which will make all prints equivalent to syswrites?

I think so.

        hp

Similar ThreadsPosted
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? February 22, 2005, 6:03 pm
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? May 15, 2005, 11:03 am
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? July 31, 2005, 10:03 am
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? October 2, 2005, 4:03 am
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? January 15, 2006, 11:03 pm
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? July 24, 2006, 9:03 am
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? November 14, 2006, 9:03 am
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? December 28, 2006, 3:03 am
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? February 14, 2007, 3:03 pm
FAQ 5.1 How do I flush/unbuffer an output filehandle? Why must I do this? May 1, 2007, 9:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap