jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

The subject says it all, the output file is full of little squares and the html it is supposed to have. It works fine as an SSI but it grows exponentially so I can't use it. Additionally when the line with the flock isn't commented out I get a compilation error, any idea why?
TIA
jg
open (FH, "+< $path_to_text") or die "where's the damn file? : $!"; #flock (FH,LOCK_EX) or die "Couldn't flock: $!"; #why does flock result in compilation errors? my @old_file = <FH>; truncate (FH,0) or die "Can't truncate: $!"; print FH "<TR> <TD> <H3> $topic by <a href=mailto:$name\@sludgefac +tor.com>"; print FH "$name</a> </H3> </TR> <P>\n"; print FH "<TR> <TD> $words <p> </TD> </TR><P>\n"; print FH @old_file; close FH or die "close damn you : $!";

_____________________________________________________
My name is Inigo Montoya...
  • Comment on Wht is my .txt file full of garbage? And why does this flock cause a compilation error?
  • Download Code

Replies are listed 'Best First'.
Re: Wht is my .txt file full of garbage? And why does this flock cause a compilation error?
by rchiav (Deacon) on Nov 14, 2001 at 06:59 UTC
    I'd guess that rob_au is correct about importing the constants. Read flock to see how to import the constants.

    As for the garbage in the file.. there's one thing that I see that could be causing it. When you read in the data, where's the file pointer? You then truncate the file. I'm not positive, but I think your pointer is beyond the end of the file.

    Before you truncate it, do a

    seek FH, 0, 0;
    I did a quick test and without the seek, I couldn't see anything in the file. When I used seek, everything printed fine.

    Hope this helps..
    Rich

      Thanks!! The seek did the trick!
      TIA
      jg
      _____________________________________________________
      My name is Inigo Montoya...
Re: Wht is my .txt file full of garbage? And why does this flock cause a compilation error?
by rob_au (Abbot) on Nov 14, 2001 at 06:47 UTC
    Okay ... here is my shot at it ...

    1. With regard to the flock(FH, LOCK_EX) statement, are you exporting the constants for LOCK_EX from Fcntl? (eg. use Fcntl qw/:DEFAULT/;) If not, then you will have to use the corresponding values for these constants, rather than their name. eg. flock FH, 8 instead of flock FH, LOCK_EX
    2. As for the 'junk' ending up in your text file, my best guess on this is because you are using the " quote for your print statements which will result in the arguments to be interpolated and thus, rather than <TR> <TD> being printed to the file, the interpolated contents of the (non-existent) file handles TR and TD are printed. To correct this, change your "'s to use the non-interpolated quotes '.

    Update - Apparently brackets are not interpolated within double-quotes .. Thanks for the save chipmunk :)

     

    Ooohhh, Rob no beer function well without!

      Rather than rewriting the whole thing so the variables still get interpolated, can I just have lines like this:
      print FH  '<TR> <TD>'.$words.'<p> </TD> </TR><P>\n';
      TIA
      jg
      _____________________________________________________
      My name is Inigo Montoya...