Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: Flushing the print buffers.

by lidden (Curate)
on Feb 21, 2008 at 23:15 UTC ( #669416=note: print w/replies, xml ) Need Help??

in reply to Re: Flushing the print buffers.
in thread Flushing the print buffers.

Is it possible with IO::Handle or otherwise to make printing to terminal fully buffered? And then do the flushing manually with $io->flush when I want the output.

I once wanted to do this to see if it was a noticeable optimization, but it was not important enough to spend time on. It is still not important but I would like to know anyway if someone happens to know.

Replies are listed 'Best First'.
Re^3: Flushing the print buffers.
by ikegami (Pope) on Feb 22, 2008 at 00:07 UTC

    ->autoflush(1) will flush after every print/syswrite/etc.
    ->autoflush(0) will flush on demand (->flush), when the buffer is full (all handles) and when "\n" is encountered (when -t HANDLE only).

    If you want something else, you'll need a tied handle or your own print function.

      What I want is a handle that is fully buffered when -t HANDLE is true, not just line buffered.
Re^3: Flushing the print buffers.
by almut (Canon) on Feb 22, 2008 at 00:22 UTC

    Good question.  With older Perls (pre-5.8) you could do

    use IO::Handle '_IOFBF'; my $buf; # set fully buffered, with a buffer size of 1024 bytes STDOUT->setvbuf($buf, _IOFBF, 1024);

    but these days, setvbuf/setbuf are no longer available unless perl is compiled to explicitly not use perlio.

    I'd like to know myself whether there is a substitute for setvbuf (without writing your own XS), as on rare occasions there might be valid uses for being able to control buffering mode or buffer size with Perl. (The function doesn't seem to be POSIX, so use POSIX; wouldn't help, AFAIK...)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://669416]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-07-21 15:57 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (449 votes). Check out past polls.