i-robot has asked for the wisdom of the Perl Monks concerning the following question:

The following code doesn't work:
#!G:\Perl\bin\perl.exe -w use strict; print "$ENV{SERVER_PROTOCOL} 200 OK\n"; print "Server: $ENV{SERVER_SOFTWARE}\n"; print "Content-type: text/plain\n\n"; print "test ok!"
The following code does work:
#!G:\Perl\bin\perl.exe -w use strict; print "Content-type: text/plain\n\n"; print "test ok!" print "$ENV{SERVER_PROTOCOL} 200 OK\n"; print "Server: $ENV{SERVER_SOFTWARE}\n";
Why???

Replies are listed 'Best First'.
Re: HTTP header
by ptum (Priest) on May 26, 2006 at 14:21 UTC

    If you want something to show up in a browser, the very first thing that is printed must be the header. That tells the browser what kind of document it is (so it doesn't interpret it as some other MIME type).


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: HTTP header
by gellyfish (Monsignor) on May 26, 2006 at 14:27 UTC

    It won't work the first way because you aren't specifying 'no parsed headers' to the server, so the server is trying to add the "HTTP/1.1 200 OK" or whatever and obviously this will give rise to an error. You will have to work out how to cause your server software to give you "non parsed headers".

    /J\

Re: HTTP header
by Herkum (Parson) on May 26, 2006 at 14:24 UTC

    You need to print the content header so that the client application knows what to do with the data it is recieving. By printing,

    print "Content-type: text/plain\n\n";

    You are telling the browser, "hey this just text data" and the browser knows what to do. When you are sending data before the content header the browser says "WTF?" and does not know what to do. So browsers may do what you expect but you can be sure that all browsers will not do the same thing.