Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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 (Patriarch) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://669416]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2023-12-06 18:14 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (31 votes). Check out past polls.