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

Hello Again. New To Perl. I have Perl script that just connects to database. It works fine running at the Perl prompt but does not run through localhost. It gets this error in the Appache log:
"Mal formed header from script. Bad header:connect to masslottery_db".
#!C:\perl\bin\perl -w # Script: connectdbtest.pl Test To Connect To A Database. use strict; use DBI; my $dbh = DBI->connect("DBI:mysql:masslottery_db", "root", "system1"); die "Connect to masslottery_db failed: " . DBI->errstr() unless $dbh; print "connect to masslottery_db successful!\n"; $dbh->disconnect();
I have other test scripts that do run through localhost but all they do is print a message. They don't do anything with DBI.

Should I be looking at some sort of setting for Apache? Thank you.

Replies are listed 'Best First'.
Re: Perl On Server
by cwry (Monk) on Jul 12, 2006 at 01:17 UTC

    Hi.

    As you can see from the apache log error, "Bad header: Connect to masslottery_db...", it's clear that the apache server was expecting your Perl script to output a header. All CGI programs need to output a header such as "Content-type: text/html" followed by a blank line.

    This can either be explicitly outputted like so:

    print "Content-type: text/html\n\n";
    Or using a module such as CGI.

    Also, as seen from the log error of "Mal formed header from script. Bad header:connect to masslottery_db"., it's clear it was able to connect to the MySQL database, because you used a lowercase 'c' in "connect to masslottery_db successful" and an uppercase 'C' in "Connect to masslottery_db failed". :)

Re: Perl On Server
by SadPenguin (Novice) on Jul 12, 2006 at 01:31 UTC
    As the above poster mentioned, it is required to output proper HTTP headers. You *can* do so manually using print, but IMHO, that is a very not-good idea. I strongly suggest spending a few hours getting used to the CGI module; once you do, you won't look back, and if you do look back, everything without CGI will seem much more labor intensive than it needs to be (since CGI does a lot of stuff for you).
Re: Perl On Server
by mrbbking (Hermit) on Jul 12, 2006 at 18:59 UTC
    From the error message, you can see that the text Apache doesn't like starts with "connect". The string you pass to 'die' is capitalized, so if the database connection failed, you'd see "Connect" in the error message. So your database connection is fine. Your only problem at this point is, indeed, the HTTP headers.
    s!!password!;y?sordid?binger?; y.paw.mrk.;;print chr 0x5b ;;; print;print chr(0x5b+0x2);;;;;