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

I've got a very simple Perl CGI script:

#!/usr/bin/perl -w # The shebang is correct, so don't write back and ask print "print "Content-type: text/html\n\n"; print "<html><body>\n"; print "<hr>Hello, world!<br><hr>\n"; print "</body></html>\n"; # You might recognize this code as a cut-paste # from hellocgi.cgi bundled with Apache

The code throws a 500 Internal Server Error. I've already made sure of the following:
My guess is that there's something that needs to be done. I pass the plate of wisdom asking for someone to tell me what to do!

Thanks,
Petras

Replies are listed 'Best First'.
Re: Wanting Validation for Smacking my Sysadmin (500 error problem)
by snowcrash (Friar) on Jan 23, 2003 at 07:12 UTC
    print "print "Content-type: text/html\n\n"; ^^^^^^
    I assume this line is some kind of copy & paste error?
    snowcrash
      You're right--it was a cut and paste error. What actually got the silly thing to work was

      print "HTTP/1.1 200 OK";
      Thanks.
Re: Wanting Validation for Smacking my Sysadmin (500 error problem)
by dws (Chancellor) on Jan 23, 2003 at 06:13 UTC
    Before you can lay down that smack, you need to triple-check that the path to Perl really is /usr/bin/perl (and not something like /usr/local/bin/perl), and that the .htaccess in that directory includes
    Options ExecCGI
      There is such a file now. I sent it as ASCII, but still I am not having any luck. Of course, it's my first .htaccess file. Would it be just a plain text file with the line

      Options ExecCGI

      Or are there any headers, etc. that need to be in place?

      How about file permissions? 755 might be too generous, but it should be okay, right?

      Thanks.

      Update: I thought there was such a file now. But it doesn't look like their is. Micro$oft Window$ won't let me save a file with only an extension--it want's a file name. I uploaded _.htaccess to the server, followed by mv _.htaccess .htaccess which I later realized made the file disapear. Any ideas for a newbie?

      Thanks.
        The webserver must be configured to allow you to override "Options", or else, this won't do a thing. See this tutorial for an explanation.
Re: Wanting Validation for Smacking my Sysadmin (500 error problem)
by robartes (Priest) on Jan 23, 2003 at 06:40 UTC
    Hi Petras,

    could it be that you simply need to include,

    print "HTTP/1.1 200 OK"
    change your first line (now second line) to:
    print "Content-type: text/html\n\n";
    or if all else fails, unbuffer STDOUT:
    local STDOUT; select((select(STDOUT), $|++)[0]);
    The permissions on the script are OK, if a bit overly generous (755 would do, or even 555). The Perl script does get executed (hence the 'Premature end of script headers', otherwise you'd get a variation on 'Command interpreter not found'), so you're looking at a problem with the script itself.

    Update: Oh, and it's never a good idea to go around smacking sysadmins. They usually have way bigger things to smack you back with, especially the ones of the BOFH variety :).

    CU
    Robartes-

      The bit of code

      local STDOUT; select((select(STDOUT), $|++)[0]);

      threw back a 'can't modify constant' error. BUT,

      print "HTTP/1.1 200 OK"

      made the script work fine (well, I never tried it verbatim--I added a semicolon to the end c",) )

      So I should probably be happy (which I am), but now you've got to tell me what the little snippet means.

      Thanks! -Petras
        The snippet unbuffers the I/O to STDOUT, by setting $| to true. Running it, I just noticed that you can't do local STDOUT;, as STDOUT is not a variable, but a constant referring to file descriptor 1. The snippet works without that part.

        What it does, working inside out, is:

        $|++;
        set currently selected file descriptor to unbuffered I/O
        select(STDOUT);
        make STDOUT currently selected file descriptor. This returns the previously selected FD.
        select ( ( select(STDOUT),$++)[0]);
        Select the first value in the list (select(STDOUT),$|++), which is the return value of select(STDOUT), which is the previously selected file descriptor.

        The snippet basically sets STDOUT to unbuffered I/O whilst preserving the previously selected filehandle as selected. Have a look in perlfaq for a better explanation.

        CU
        Robartes-

Re: Wanting Validation for Smacking my Sysadmin (500 error problem)
by dda (Friar) on Jan 23, 2003 at 06:35 UTC
    Check the line endings - are they UNIX or DOS ones?

    --dda

      Cool, I'm about to learn something....

      What are the differences between UNIX and DOS line endings? Every line ends with a semicolon, of course. I used ASCII when I transfered the file. What's the difference between the two? How do I make sure I'm using the right one?

      Thanks.
        Every line in DOS text file ends with "0D0A" sequence, while UNIX file contains only "0A" at the end of each line. You can check it with 'od' command:

        od -t x filename.pl

        --dda

Re: Wanting Validation for Smacking my Sysadmin (500 error problem)
by Cody Pendant (Prior) on Jan 23, 2003 at 06:21 UTC
    What do you mean by "runs locally through Apache--just not over my web server" exactly? What's the URL when it's "running locally"?
    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.” M-J D
      The local URL is

      http://localhost:4444/cgi-bin/hellocgi.pl

      That's when Perl Console is running (from Indigo Perl).
Re: Wanting Validation for Smacking my Sysadmin (500 error problem)
by Popcorn Dave (Abbot) on Jan 23, 2003 at 06:16 UTC
    I'm no Unix expert, but shouldn't that be set to 755 instead of 775? Could that be your problem or was that just a typo?

    There is no emoticon for what I'm feeling now.

      Popcorn Dave - While the permissions you suggest (755) would certainly be preferrable, that would not cause the 500 error. 755 indicates read-write-execute for the owner(7) and read-execute for the group(5) and the world(5). 775 indicates read-write-execute for the owner(7) and the group(7) and read-execute for the world(5). You can find a more detailed explanation of UNIX permissions here.

      -- vek --
Re: Wanting Validation for Smacking my Sysadmin (500 error problem)
by vek (Prior) on Jan 23, 2003 at 13:52 UTC
    Petras, snowcrash hit the nail on the head. You've got a mistake on this line:
    print "print "Content-type: text/html\n\n";
    Should be:
    print "Content-type: text/html\n\n";
    -- vek --
      It was just a cut and paste error. It does remind me a little, though, of times when I obfuscatingly used JavaScript to write other Java Script code. Comes out kinda nasty ;)

      -Petras
Re: Wanting Validation for Smacking my Sysadmin (500 error problem)
by helgi (Hermit) on Jan 23, 2003 at 13:29 UTC
    Have a look at the web server's error log (or more likely, ask your SysAdmin to let you look at it.

    He may not let you (a lot of them don't), but you can return the error messages to the browser using:

    use CGI::Carp 'fatalsToBrowser';

    See 'perldoc CGI::Carp' for further information.

    --
    Regards,
    Helgi Briem
    helgi AT decode DOT is