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

I'm a newbie so please go lightly on me guys.. Can someone explain to me why I get a server error with this simple script?
#!/usr/local/bin/perl -i open(F,"+< $new_guestbook.html") or die "Could not open F: $!"; $out = ''; while(<F>){ s/date/localtime/eg; } seek(F,0,0); print F $out; truncate(F,tell(F)); close(F);

Replies are listed 'Best First'.
Re: CGI Server Error
by monkeygirl (Pilgrim) on Jul 03, 2001 at 23:48 UTC

    If this is a CGI script, you really need to print a header:

    print "Content-type: text/html\n\n";

    You can also do this with CGI.pm:

    use CGI; my $use_me_im_better = new CGI; print $use_me_im_better->header;

    And while you're in there, you should also do

    use CGI::Carp qw(fatalsToBrowser);

    This will print the error messages on the screen.


    Sarah
    If Bill Gates can name a company after his "bedroom" problems, I can have a stupid sig that points it out

(jeffa) Re: CGI Server Error
by jeffa (Bishop) on Jul 03, 2001 at 23:50 UTC
    Server error? Is this a command line Perl script or a CGI script? If it's CGI, don't forget that the first thing you have to print to STDOUT is the header:
    print "Content-type: text/html\n\n"; # rest of code
    Or better yet, use CGI!
    use CGI ':standard'; print header(); # rest of code
    And as an added bonus, here is a one-liner that will replace the string 'date' with localtime:
    perl -pie 's/date/localtime/eg' guestbook.html

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: CGI Server Error
by tachyon (Chancellor) on Jul 04, 2001 at 00:25 UTC

    From your post the context is not clear but there are some problems with your code. First you don't really need the -i in this context see perlman:perlrun. Your open opens the file for reading and writing. This is fine. You set $out to '' which is fine. You then iterate over your file handle assigning each line to $_. You modify $_ substituting the string 'date' with localtime() - this is fine but you don't actually do anything with the result. You then seek the start of the file, print a null string ($out) to it and truncate. You don't check the return values of seek, truncate or tell which is generally considered good form. Anyway the net result is your code will erase the entire contents of the target file. If the aim of this code is to open a file and change all the strings 'date' to the value of localtime then this works:

    #!/usr/local/bin/perl -w use strict; my $file = "c:/test.txt"; open(F,"+<$file") or die "Could not open $file: $!"; my @file = <F>; s/date/localtime/eg for @file; seek F,0,0 or die "Could not seek in $file: $!"; truncate F,0 or die "Could not truncate $file: $!"; print F @file; close F;

    I hope this helps.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

file opening question
by deriwana (Initiate) on Jul 04, 2001 at 01:14 UTC
    Newbie question here (so please go easy on me).. Could someone tell me why nothing happens when I access this script through my browser? Ultimately, I want to read an html file into my browser, edit and/or append to the file, and save the changes. So far, I just get a blank screen.
    #!/usr/local/bin/perl use strict; use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; my $file = "new_guestbook.html"; open(F,"+< $file") or die "Could not open $file: $!"; my @file = <F>; s/date/localtime/eg for @file; seek F,0,0 or die "Could not seek in $file: $!"; truncate F,0 or die "Could not truncate $file: $!"; print F @file; close F;

      You forgot print $file; which sends the modified contents to the browser. Your code modifies the file but never sends anything other than the header to the browser.

              - tye (but my friends call me "Tye")

        Unusual to be able to correct tye but you actually need print @file to print the file contents as $file is just the file name. To clarify print F @file goes to the file handle we have opened called <F>. print @file goes to the default file handle (in this case STDOUT) which is what gets sent to the browser.

        tachyon

        s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Do you mean the file update doesn't happen, or that you don't see anything in your browser?

      Other than the http header, you're not sending anything to the browser.