tretin@school has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, this is my first post (so please bare with me); I need help!

I'm trying to run a script I wrote at school on an Apache server. It does not run, and I get the following error in my server log:

| malformed header from script. Bad header=test test: c:program files/etc. etc

I'm running a very simple script:

#!perl
print "test\n";

I'm very new to the world of Perl and any assistance would be greatly appreciated.

Replies are listed 'Best First'.
Re: Malformed Header?
by Masem (Monsignor) on Oct 24, 2001 at 17:52 UTC
    When you send a page via HTTP, there is a special header that you must send that tends the client what type of document you are sending. This is followed by two returns (\n\n), at which point you can then send your HTML or other text.

    You can generate this header using the header() function in the CGI.pm module. It's probably a good idea to browser through the documentation of this module as well as there's a lot to be learned from it on how to dynamical create HTML documents.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      Thanks a bunch, I'm going to try that asap.

      Much appreciated!
Re: Malformed Header?
by Zecho (Hermit) on Oct 24, 2001 at 17:52 UTC
    #!perl -w use strict; print "Content-type: text/plain\n\n"; print "test\n";
    make sure there's a newline after the print statement.
Re: Malformed Header?
by DrManhattan (Chaplain) on Oct 24, 2001 at 17:55 UTC
Re: Malformed Header?
by ajt (Prior) on Oct 24, 2001 at 18:14 UTC
    This is a small aside, but the path c:program files suggests that your running on a Windowds box. Apache on Windows by default needs a shebang line as if it were on Unix. So if your copy of Perl is in c:\Perl, the first line should read: #!c:/perl/bin/perl

    Now if Apache has been configure to use the registry hack then please ignore this comment, as Apache will not care what the shebang line is.

    The other comments here are fine, if Apache doesn't get the two carriage returns between the HTTP headers and the body content it will die on you.

    If you are new to Perl be warned that Perl/CGI while fun, is like learning to drive on the Autobahn, you may find an empty country lane somewhat safer.

    You should look at the O'Reilly's Rat Book, and have a look at Lincon Stein's CGI module, it will take a lot of pain out of CGI for you. Check out the Book Reviews section, you will see that there are quite a few books to AVOID!

Re: Malformed Header?
by sevensven (Pilgrim) on Oct 24, 2001 at 18:09 UTC
    You need to add
    print "Content-type: text/html\n\n";
    before the
    print "test\n";

    You could check this simple article in devdaily.com, the Common Gateway Interface specification and, if you can, the use of CGI.pm

    This sorry excuse for a monk would also sugest a switch from cgi to mod_perl, for better performance and more features, in the long run, if you could get the apache admin to set it up

    Update: and you should drink from the monks knowledge in the Q&A about CGI programming

Re: Malformed Header?
by tachyon (Chancellor) on Oct 24, 2001 at 18:37 UTC

    CGI Help Guide

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print