Cargo Cult Advising

chromatic on 2006-08-09T01:22:48

I swear, I really do, that I'm really not responsible for what happens the next time someone says "Maybe you're suffering from buffering!" without noticing that the code prints to standard output and includes a newline character.

If you don't understand the problem, please refrain from giving a misleading and unhelpful answer -- you aren't helping the person with the problem and you're spreading misinformation that makes it more difficult to give people the right answers.

It's not difficult to understand. I even wrote Unnecessary Unbuffering to help you understand.


A question then...

sigzero on 2006-08-09T02:46:18

I see some of merlyn's cgi code containing "$| = 1;" but I never understood what it was for.

At least, that is where I think I saw it...

Re:A question then...

chromatic on 2006-08-09T03:26:11

Yep, it's part of the standard header in all of his programs, not just his CGI ones. (If you read more than one of his columns, he copies and pastes that explanation.)

I think it's an unnecessary pessimization in terms of clarity and performance. Most of the time, buffering isn't a problem. Only change the default when it is.

only when necessary

Crag on 2006-08-09T13:42:26

"Most of the time, buffering isn't a problem. Only change the default when it is."

This seems like an excellent rule of thumb even for a more general context. Maybe I'll leave "use strict; use warnings;" out of my templates and only put them in code while its tests aren't passing.

(I'm only about 20% serious.)

Re:only when necessary

chromatic on 2006-08-09T17:13:18

Ah, but my default rule of thumb has an implicit "Assuming the default is good and useful..." preface. I can see now that my buffering rule of thumb is one of the 20% of cases where that clause should be explicit.

Re:A question then...

merlyn on 2006-08-12T14:35:46

It's probably partly legacy, partly defensive. In the "old days", if you printed trace messages, you never saw them unless you unbuffered output. And I frequently print trace messages while I'm "debugging" (as I describe in my debugging web apps column).

Nowadays, I tend to use only "warn" for trace messages, so perhaps I should change my default style for non-CGI things.

Ah, but...

Adrian on 2006-08-09T07:08:47

If you don't understand the problem, please refrain from giving a misleading and unhelpful answer

The problem with this advice is that people have to know that the don't understand the problem...

It sounds like your message is not getting out

grantm on 2006-08-09T09:53:25

Maybe you need to turn off buffering :-)

Re:It sounds like your message is not getting out

runrig on 2006-08-10T02:57:59

I hope you checked for STDOUT and a newline before you said that...I fear the consequences otherwise :-)

And don't forget that

runrig on 2006-08-10T20:15:27

Reading from STDIN flushes STDOUT, whether or not there was a newline. Though I'm told it behaved slightly differently in past versions of ActiveState perl (5.6.something?) on Win32 before it was compiled with USE_PERLIO.