Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Flushing the print buffers.

by ikegami (Pope)
on Feb 21, 2008 at 20:23 UTC ( #669385=note: print w/ replies, xml ) Need Help??


in reply to Flushing the print buffers.

Don't really understand that code

I use the more readable

use IO::Handle qw( ); # For autoflush OUTPUT_HANDLE->autoflush(1);

If you want to flush without turning on autoflush,

use IO::Handle qw( ); # For flush OUTPUT_HANDLE->flush();

(You only need to load IO::Handle once in your program, not everytime autoflush or flush are called.)


Comment on Re: Flushing the print buffers.
Select or Download Code
Re^2: Flushing the print buffers.
by lidden (Deacon) on Feb 21, 2008 at 23:15 UTC
    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.

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

      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://669385]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-08-20 15:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (116 votes), past polls