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

i have this code which doeswnt want to work:
@gb = <GB>; #where GB is a guestbook html file @gb2 = ("jsdjsjsj"); foreach $line(@bg){ push(@gb2,$line); if($line =~ /INSERT_NEW_ENTRY/){ push(@gb2,"<b>entry</b> $FORM{'whatever'}"); } } seek(GB,0,0); truncate(GB,0); print GB @gb2;
When I execute this, GB becomes "jsdjsjsj" and not that and whatever was pushed into it. What could I be doing wrong?

Replies are listed 'Best First'.
Re: pushing
by davorg (Chancellor) on Dec 30, 2000 at 18:29 UTC

    Could it be that instead of

    foreach $line (@bg) {

    you actually meant

    foreach $line (@gb) {

    Using use strict and -w is a far quicker way of finding these sorts of errors than posting here.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      No that was a typing error in the submission and it is @gb in the actual script. It is something else

        OK. Next guess would be that you're not checking the results of the GB open call (which you haven't shown us) and that therefore your @gb array ends up empty.

        What gets written to the error log? Have you tried running the script from the command line?

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

Re: pushing
by lhoward (Vicar) on Dec 30, 2000 at 18:36 UTC
    always "use strict" and enable warning. That would have found this problem immediately. You named one of your variables incorrectly. Your code should read (the change is in bold):
    
      @gb = <GB>; #where GB is a guestbook html file
      @gb2 = ("jsdjsjsj");
      foreach $line(@gb){
        push(@gb2,$line);
        if($line =~ /INSERT_NEW_ENTRY/){
          push(@gb2,"<b>entry</b> $FORM{'whatever'}");
        }
      }
      seek(GB,0,0);
      truncate(GB,0);
      print GB @gb2;
    
    
      use strict -w; doesnt do anything!!

        I think you'll find they do if you read your error log!

        Try adding

        use CGI::Carp qw(fatalsToBrowser);

        to the script.

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

        This problem is a great example of why use strict is such a lifesaver. But perhaps lhoward was overly subtle. He provided a corrected code fragment without calling attention to the correction.

        Read his code again, carefully. If necessary, copy/paste it into a file and diff it against your source.

        If you're already doing use strict and aren't getting any error messages, then there's a different type of programming lesson waiting for you as when you compare the code snippets.

Re: pushing
by jeroenes (Priest) on Dec 30, 2000 at 18:48 UTC
    You have to reopen your filehandle for writing. So before the seek, you should have something like:
    close GB; open( GB, ">$file");
    You than can even leave the seek and truncate out.

    Hope this helps,

    Jeroen
    I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)