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

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

Replies are listed 'Best First'.
Re^2: Flushing the print buffers.
by lidden (Curate) 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?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2018-09-22 18:58 GMT
Find Nodes?
    Voting Booth?
    Eventually, "covfefe" will come to mean:

    Results (190 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!