What am I working on right now? Probably the Sprog project [sourceforge.net].
GnuPG key Fingerprint:
6CA8 2022 5006 70E9 2D66
AE3F 1AF1 A20A 4CC0 0851
A colleague asked me to help him debug a bit of code (running under mod_perl) that looked something like this:
open TEST, ">$filepath";
print TEST "A test message\n";
His symptom was that the file would get created but nothing would get written to it - usually. Sometimes "inexplicably" something would get written but there was "no pattern" to when it worked and when it didn't.
Ignoring for a moment the capital offense of failing to check the return value from open (which was fine as it happens) and the fact that he ought to be using the three argument open ($filepath was trusted in this case), can you see the problem?
He forgot to close the file, so the one line output from print stayed in the buffer. Since he was using a global filehandle it never went out of scope and since it was running under mod_perl, the Perl interpreter never exited so Perl never closed it for him. If he had run it repeatedly, each open would have caused an implicit close and buffer flush but since he kept making 'random changes' he was never quite sure what made it work.