perl problem - i cannot open a file for writing


hello there perl gurus...

i have this portion of a guestbook cgi script:

open (FILE,">$datafile");

when i issue this command, i get the following message from the web browser:

500 Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

here is what the error_log said:

[Tue Jun 5 19:58:29 2001] [error] [client] Premature end of script headers: /Library/WebServer/CGI-Executables/signgbook.cgi

let me know if you can help! :)

That problem occurs when your script doesn't generate a proper mime header. The output of all CGI's must begin with a content type line followed by a newline character, like:

Content-type: TEXT/html\n

Your script isn't set up to die or issue a warning that would indicate whether the the open command is successful or not. So it's impossible to tell (without knowing more about your program) whether the error is related at all to opening the file. If you use warn, die, or othewise write to STDERR from a perl cgi, that message will end up in the web server error log, unless you deliberately re-open STDERR to point at a different file.

With regard to the file opening, permissions are going to be important. Web servers usually run as "nobody" or "www" which typically has very restricted access to stuff, for security reasons. You must be creating the file in a place where www has write access (the directories in the path must be executable too). If your file is not even being created, this is the first thing to look at.


thanks for the reply! i have been trying to get this guestbook program (megabook) running, and it is having a problem with that line. if i remove the "<" it works fine. all the other items you metion are all set. i have the permissions all set up properly, and it still dies because of that one character...

can you offer any more help? i could post the whole file if you like...

thanks again,
Nah, don't post the entire code yet. But lets get down to a useful error message.

Could you change the line that reads

open (FILE,">$datafile");


open (FILE, ">$datafile") or warn "Can't open file $datafile: $! ";

This should give you a more useful error message that we can work from.

thanks again for the continued help... :)

i changed the cgi code, and i am still getting the same error... :(

any other ideas?

Well you should get the same error ;) but you should also get the warning message from the cgi in your error log, which is what we need to look at. If you didn't, then there is definitely something odd going on.

Does the program close STDERR or reopen it to some other file? If so, that's where the warnings will go, and you'll want to check that file...

No other good ideas right now without seeing the code...


thanks again for the help! attached, please find the file (txt extension to be able to upload...

thanks again


  • signgbook.txt
    14.6 KB · Views: 4
i don't have any real good ideas why it isn't working.

you might look for a lock file (gbdb.lock) to see if it exists and is screwing you up.

How did you narrow it down to the line you flag as the erroneous line?

I don't see any re-direction of standard error, so if that command were failing, the error message Can't open file $datafile: $! should appear in the web server's error log. If it's not there, that line is probably successful.

You also might want to take a look at the block that starts:

if ($success) {

print "Location: $success \n\n";

} else {

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

print <<SUCCESS;

$success is a variable read in from a config file "setup.db" If it is set to true, it won't print the Content-type header, which could be a problem. Though I can't really see why that would have anything to do with whether or not youre opening for writing in the
open (FILE, ">$datafile") or warn "Can't open file $datafile: $! ";

Not much more I can do short of trying to install this package on my own machine.