I was refactoring some old code (by other people) and I came across the following at the top of some CGI scripts:
#Turn on output buffering
local $| = 1;
perlcritic as usual unhelpfully points out the obvious : “Magic punctuation used”. Are there any alternatives to this or perlcritic is just grumpy?
Furthermore, on closer inspection. I think the code is wrong.
If I’m not mistaken, it means exactly the opposite from what the comment says. It turns off the output buffering. My memory is a little bit rusty and I can’t seem to find the Perl documentation that describes this magic punctuation. The scripts run in mod_perl.
Does messing around with Perl’s buffering behavior desirable and results in any performance gain? Most of the stuff written about this comes from the early part of the first decade of the 21st century. Is this still even a valid good practice?
Your question seems a bit scattered, but I’ll try my best to answer thoroughly.
You want to read
perldoc pervar. The relevant section says:So yes, the comment is incorrect. Setting
$| = 1does indeed disable buffering, not turn it on.As for performance, the reason output buffering is enabled by default is because this improves performance–even in 2011–and probably until the end of time, unless quantum I/O somehow changes the way we understand I/O entirely.
The reasons to disable output buffering are not to improve performance, but to change some other behavior at the expense of performance.
Since I have no idea what your code does, I cannot speculate as to its reason for wanting to disable output buffering.
Some (but by no means all) possible reasons to disable output buffering: