Setting perl"s output buffer size

Setting perl"s output buffer size

am 16.10.2007 11:42:57 von Itay Greenspon

Hi,

When printing to an output handle, buffering varies with the type of
output device.
>From the perl cookbook:
"Disk files are block buffered, often with a buffer size of more than
2K. Pipes and sockets are often buffered with a buffer size between
1/2 and 2K. Serial devices, including terminals, modems, mice, and
joysticks, are normally line-buffered; stdio sends the entire line out
only when it gets the newline."


There's a simple mechanism to enable autoflushing (using $| or
otherwise) ,
But can one control the SIZE of the output buffer?
(rather than setting buffering "on"/"off")

-- Itay

Re: Setting perl"s output buffer size

am 16.10.2007 15:24:38 von Ben Morrow

Quoth Itay Greenspon :
>
> When printing to an output handle, buffering varies with the type of
> output device.
> >From the perl cookbook:
> "Disk files are block buffered, often with a buffer size of more than
> 2K. Pipes and sockets are often buffered with a buffer size between
> 1/2 and 2K. Serial devices, including terminals, modems, mice, and
> joysticks, are normally line-buffered; stdio sends the entire line out
> only when it gets the newline."
>
>
> There's a simple mechanism to enable autoflushing (using $| or
> otherwise) ,
> But can one control the SIZE of the output buffer?
> (rather than setting buffering "on"/"off")

Why do you want to? The buffer is chosen to be an appropriate size to
get efficient IO.

With perls before 5.8, you can use IO::Handle::setvbuf to set the size
of stdio's buffers. After 5.8 perl doesn't use stdio by default, so the
size of the buffer cannot be set. If you have a need to control the
buffering that accurately, you are best off opening the file with a
:unix layer (see perldoc PerlIO) to get unbuffered IO, and doing the
buffering yourself.

Ben

Re: Setting perl"s output buffer size

am 19.10.2007 00:05:17 von Itay Greenspon

On Oct 16, 3:24 pm, Ben Morrow wrote:
> Quoth Itay Greenspon :
>
>
>
> > When printing to an output handle, buffering varies with the type of
> > output device.
> > >From the perl cookbook:
> > "Disk files are block buffered, often with a buffer size of more than
> > 2K. Pipes and sockets are often buffered with a buffer size between
> > 1/2 and 2K. Serial devices, including terminals, modems, mice, and
> > joysticks, are normally line-buffered; stdio sends the entire line out
> > only when it gets the newline."
>
> > There's a simple mechanism to enable autoflushing (using $| or
> > otherwise) ,
> > But can one control the SIZE of the output buffer?
> > (rather than setting buffering "on"/"off")
>
> Why do you want to? The buffer is chosen to be an appropriate size to
> get efficient IO.
>
> With perls before 5.8, you can use IO::Handle::setvbuf to set the size
> of stdio's buffers. After 5.8 perl doesn't use stdio by default, so the
> size of the buffer cannot be set. If you have a need to control the
> buffering that accurately, you are best off opening the file with a
> :unix layer (see perldoc PerlIO) to get unbuffered IO, and doing the
> buffering yourself.
>
> Ben

Thanks ben, I'll try the unix layer.
btw,
I need this because I'm using a custom (non standard) IO device.