in reply to (Ovid) Re: dbi 500 error
in thread dbi 500 error

I'm kinda new at Perl myself (being reformed from days as an amatuer Java geek). Chromatic (an old college friend) refered me to perlmonks. I have moved to a new server and am consitently getting 500 errors trying to run simple scripts (even hellocgi.cgi that comes with Apache bundled with Indigo Perl). I can execute the scripts fine, locally, using Apache, but on my server they consistently throw 500 errors. File permissions are set to 775, and the shebang is all good. I can't get it to work, but Ovid said something that made me want to ask a question:

Ovid wrote:
You're not sending any output back to the client, so your error log is probably complaining about "Premature end of script headers". That doesn't mean your program is wrong, just that you either need to run it from the command line or convert it to a proper CGI program.

The program works from the command line (obviously, PuTTY won't render the HTML). And it works locally. The phrase "convert it to a proper CGI program" caught my attention, though. In Java everything got compiled into classes. I can't imagine needing to do that to a Perl script. What did you mean by the statement?

Thanks,
Mike

Replies are listed 'Best First'.
Converting to a CGI script
by Ovid (Cardinal) on Jan 23, 2003 at 18:10 UTC

    Hi Mike,

    Note: this code is untested.

    What did you mean by [convert it to a proper CGI program]?

    That's a straightforward question, but regrettably, the answer is not as clear. Typically, when working with in a CGI environment, several issues arise that may not occur in different environment. You have to worry about sending headers, formatting your output correctly, maintaining state, etc. In other words, consider what happens if you want a simple "hello, world" program:

    perl -le 'print "Hello, world!"'

    And if I want to personalize this:

    perl -e 'print "Hello, ", shift' Ovid

    Now, what happens if I want to convert that to a CGI program? The first example is easy, but I won't do it as a one-liner because that's not going to work with a Web server.

    print "Content-Type: text/plain; charset=ISO-8859-1\n\nHello, world"';

    That's roughly equivalent to the "Hello, world" program, but what do we do if we want to personalize it? We'll switch to CGI.pm to make our life easier. First, we assume the following form:

    <form action="/cgi-bin/test.cgi"> <input type="text" name="name"> <input type="submit" name="submit" value="Press me!"> </form>

    Then we read it and respond:

    #!/usr/bin/perl -T use warnings; use strict; use CGI qw(:standard); my $_name = param('name') || ''; my ($name) = $_name =~ /([[:alnum:][:space:][:punct:]]+)/; $name ||= 'Anonymous Monk'; print header, <<"END_HTML"; <html> <head><title>Greeting Page</title></head> <body><p>Hello, $name</body> </html> END_HTML

    And if we need persistence:

    #!/usr/bin/perl -T use warnings; use strict; use CGI qw(:standard); my $_name = cookie('name') || param('name') || ''; my ($name) = $_name =~ /([[:alnum:][:space:][:punct:]]+)/; $name ||= 'Anonymous Monk'; my $cookie = cookie( -name => 'name', -value => $name ); print header(-cookie => $cookie), <<"END_HTML"; <html> <head><title>Greeting Page</title></head> <body><p>Hello, $name</body> </html> END_HTML

    I could go on further, but I think you get the basic idea. The last listing is where I got to from a simple command line "hello, world" script to a CGI program. If you see some strange things in the last script, note my .sig line which has a link to my CGI course. That should answer many questions. In any event, I hope this answered your question.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)