Disabling output buffering in Perl

In one of my Perl modules, WWW::BrokenLinks, I wanted to push lots of small log messages to standard output, which I would then redirect to a file. Unfortunately, the combination of short messages and file redirection meant that it would take some time before the buffer was full, which caused a substantial delay between the log message being generated and it appearing in the file.

Fortunately, fixing this issue by disabling output buffering requires just two lines of Perl:

use IO::Handle;
STDOUT->autoflush(1);

Note: Generally output buffering is a sensible thing, which is why it is enabled by default. Don’t disable it unless you know you have a good reason for doing so.

 

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.