Just a simple [cpan.org] guy, hacking Perl for fun and profit since way back in the last millenium. You may find me hanging around in the monestary [perlmonks.org].
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.
IO::File (Score:2)
Of course, I then come a across a problem with implicit closes [perl.org], so I really should call close explicitly anyway...
-Dom
Re:IO::File (Score:1)
(And, hopefully, my example will show the other flaw in the program - no error handling.)
---ict / Spoon
Looks to me... (Score:1)
Then... he is running it (I assume. Bad me!) under Apache, which spawns multiple children.
Then... the first child creates the file and writes to it. Then... who knows which kid gets to run the program the next time... but, when the kid runs the program... it wipes out the existing file and starts anew.
This looks to me like someone forgot that the code will be running multiple times, simultaneously.
mod_perl, so... (Score:1)
I guess that's what Dom2 is saying, too.