in reply to Die function misfunctions

To provide a smidge more detail, when your program "dies" it throws a plain ol' line of text to STDERR (Standard Error) and nothing to STDOUT. (For a CGI, STDOUT is the web browser.) Without anything sent to STDOUT, the web browser doesn't have anything to display, so you get an Error 500.

Before anything can be sent to the browser you first have to tell it what kind of data to expect. The "kind of data" (called the "MIME" type) is sent when you send the "Content-type" header, as in

print "Content-type: text/html\n\n"; # or, same thing but better use CGI qw (:standard); print header;
If a proper "Content-type" string isn't the first thing your browser gets from your CGI you'll see a "error 500" just like you did. "Die" doesn't send a "Content-type" header when it sends it's error message, so voila, there's your problem.

virtualsue's suggestion to use CGI::Carp qw(fatalsToBrowser); is the solution. This causes any calls to "die" to be redirected to STDOUT, with the proper content-type header, allowing you to automagically see the content of your error message. It's very cool. Use it in good health.

Gary Blackburn
Trained Killer

Correction: Corrected STDOUT to STDERR. Ooops.

Replies are listed 'Best First'.
Re: Re: Die function misfunctions
by lauragarcia (Acolyte) on Apr 30, 2001 at 20:32 UTC
    I appreciate the detail you furnished. The first lines of the script are now:
    #!/usr/bin/perl use CGI qw(:standard); print header; use CGI::Carp qw(fatalsToBrowser); $query = new CGI; etc....
    I'm still getting that silly ol' Internal Server Error message.
    laura.guimauve
      Have you tried running it from the command line? If you are still getting a 500 error, it quite likely is due to a syntax error in your script. Whenever I tinker with one of my CGI programs I always run it quickly from the editor to see if I broke anything before trying it from a browser.

      I also suggest turning on warnings and taint mode via

      #!/usr/bin/perl -wT
      and enabling strict syntax checking like so
      use strict;
      Taint mode is essential for security reasons, whereas the other things are just plain good for your development as a perl programmer. For more info on these, there is a wealth of information on this site (or just keep asking questions). You should also read 'perldoc perlsec' since you are writing CGI code.
      FYI, if you use
      use CGI qw (:standard);
      that's using CGI.pm in "function" mode, meaning there is no need to instantiate an object to use all the nifty CGI features. This is why print header; works. The "header" function gets imported from CGI into your program by the qw(:standard) bit in "use CGI." There are plenty of docs for this in the POD for CGI.pm.

      The long and the short of it is: if you're using qw(:standard) don't bother using $query=new CGI. In fact, as I have discovered on a couple of occassions, doing so can even get you into trouble. :-(

      But that's probably not your problem. Whenever you get Error 500 messages it means that your web browser is not getting what it expects, which means your CGI is not providing good, properly header'd information. If you have fatalstobrowser turned on and you're still getting the error it means your program isn't even compiling properly. (If it compiled, FatalsToBrowser would kick in and tell you what was wrong.) To check compilation do a perl -c yourscript.cgi from the command line to root out any nasty syntax errors.

      Gary Blackburn
      Trained Killer

        Reply to y'all (with my thanks!) You're right, the program has syntax errors and is not compiling correctly. Even so, what does seem to do nice error logging, however, is the
        BEGIN { use CGI::Carp qw(carpout); open(LOG, ">mycgi-log.txt") or die "Unable to append to mycgi-log: $! "; carpout(*LOG); }
        script, which is catching and printing my errors in the file and isn't too much trouble to consult. I just can't get the fatalstoBrowser to kick in.

        Also, thanks for confirming the possible conflict with the $object designation and the qw(:standard) syntax.

        laura.guimauve

      Can you look at the server's error log? I don't think it's the die at this point, maybe CGI::Carp isn't installed?

      --isotope
      http://www.skylab.org/~isotope/