manish.rathi has asked for the wisdom of the Perl Monks concerning the following question:

print << END_OF_HTML; Content-type: text/html; <HTML> <head> <title>About this server</title> </head> <body> <h1>About this server</h1> <hr> <pre> server name : $ENV{SERVER_NAME} Listening on port : $ENV{SERVER_PORT} Server Software : $ENV{SERVER_SOFTWARE} Server Protocol : $ENV{SERVER_PROTOCOL} CGI Version : $ENV{GATEWAY_INTERFACE} </pre> <hr> </body> </html> END_OF_HTML
This file is saved as server_info.cgi and put in cgi-bin directory under the path "C:\Program Files\Apache Group\Apache2\cgi-bin" and this path is configured in httpd.conf file in apache server under the scriptAlias parameter.

When I send a request "http://localhost/cgi-bin/server_info.cgi" I get the error message "500 Internal Server Error". So whats wrong in here ?

Replies are listed 'Best First'.
Re: CGI script gives "internal server error"
by japhy (Canon) on Mar 03, 2009 at 19:30 UTC
    CGI notwithstanding, the error is in your here-doc syntax. In Perl, here-docs must be created in the following ways:
    print <<NO_SPACE_BETWEEN_THE_ANGLE_BRACKETS_AND_THIS_TEXT; ... NO_SPACE_BETWEEN_THE_ANGLE_BRACKETS_AND_THIS_TEXT # or print << "SPACE_ALLOWED_WHEN_THIS_TEXT_IS_QUOTED"; ... SPACE_ALLOWED_WHEN_THIS_TEXT_IS_QUOTED
    This is documented in perlop: "There must be no space between the '<<' and the identifier, unless the identifier is quoted. (If you put a space it will be treated as a null identifier, which is valid, and matches the first empty line.)"

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    Nos autem praedicamus Christum crucifixum (1 Cor. 1:23) - The Cross Reference (My Blog)
      I removed space between <<END_OF_HTML, but still script did not run successfully.

      I checked the error.log and there I saw following entries

      "Tue Mar 03 15:10:42 2009 error client 127.0.0.1 (OS 5)Access is denied. : couldn't create child process: 720005: server_info.cgi

      Tue Mar 03 15:10:42 2009 error client 127.0.0.1 (OS 5)Access is denied. : couldn't spawn child process: C:/Program Files/Apache Group/Apache2/cgi-bin/server_info.cgi"

      What does these error messages suggest ?

        Maybe your server doesn't know to use perl to execute .cgi files? First make sure your script is executable(if on unix). Also try adding one of the following shebang lines as the very first line in your script.

        UNIX:
        #!/usr/bin/perl
        WINDOWS either:
        #!perl
        or
        #!C:\perl\bin\perl.exe

        You should substitute the path with the correct path to your perl executable

        UPDATE: Doh, obviously those are windows paths. Ignore what I said about UNIX and try "#!perl" on the first line. Also verify the file has the correct permissions, that it's readable by the webserver

Re: CGI script gives "internal server error"
by glasswalk3r (Friar) on Mar 03, 2009 at 19:23 UTC

    What do you think about looking at Apache log files to know what is wrong? There is a good change that the problem will be described there.

    Alceu Rodrigues de Freitas Junior
    ---------------------------------
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill
Re: CGI script gives "internal server error"
by perlwisdomseeker (Initiate) on Mar 03, 2009 at 20:41 UTC
    hi, i have been through these problems before so these are the posible solution i will go for, 1, can you run the default file which comes with apache( mind that it has #!/ in the correct place) 2, i use to think that Content-type: text/html should be followed by \n\n 3, try to run the file from cmd
      default file that comes with apache is running fine.

      I have put shebang line but script still gives same error

      If I run this script from cmd line, how will perl interprete header message ? If I send a request, then server will take care of the header message. Anyways, I tried to run script from cmd line and got error message

      "C:\Program Files\Apache Group\Apache2\cgi-bin>perl server_info.cgi

      Can't find string terminator "END_OF_HTML" anywhere before EOF at server_info.cgi line 3.

        I think you need a newline after the END_OF_HTML, otherwise perl won't recognize it as the heredoc terminator.
Re: CGI script gives "internal server error"
by Anonymous Monk on Mar 04, 2009 at 05:15 UTC