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

Re^2: Flushing the print buffers.

by lidden (Deacon)
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.


Comment on Re^2: Flushing the print buffers.
Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://669416]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-11-27 00:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (177 votes), past polls