Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

grantm (164)

grantm
  (email not shown publicly)
http://www.mclean.net.nz/

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

Journal of grantm (164)

Monday August 04, 2003
04:52 AM

mod_perl fun

[ #13885 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • This is one of the reasons why I try to use IO::File for everything. It just makes life much easier.

    Of course, I then come a across a problem with implicit closes [perl.org], so I really should call close explicitly anyway...

    -Dom

    • Since 5.6 you haven't even needed IO::File.
        open my $fh, '>', $filepath or die "Could not open $filepath: $!\n";
      It's great and it's built in.

      (And, hopefully, my example will show the other flaw in the program - no error handling.)
      --
        ---ict / Spoon
  • Looks to me like the file gets created every time... instead of appended.

    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.
  • The file never gets closed? The string stays in the buffer and never gets written to disk?

    I guess that's what Dom2 is saying, too.