tags:

views:

80

answers:

2

If I'm using the CGI module in Perl, I can do this

$cgi = CGI->new();
$cgi->header(-type=>"text/html");

Or go for the classic

print "Content-Type: text/html\r\n\r\n";

Does it matter which we use? Is there any difference between the two? Both seem to work.

For me I'd go for the first if I was using CGI anyway, but if not then I wouldn't bother loading the module for just one action. But just wondering if there is any thing on the subject?

Psy

+2  A: 

Strictly speaking, you have to print \r characters:

print "Content-Type: text/html\r\n\r\n"

is a legal way to express what you want to say.

Generally, I'd stick with what CGI offers. It allows much more concise and readable code, and CGI knows a lot more about such details than you do.

reinierpost
Sorry, forgot about the \r's edited code to reflect. Thanks
Psytronic
Agreed. CGI is pretty big though, and so you take a bit of a run-time speed hit. CGI::Simple will do the job a little faster (assuming you have it installed).
Gavin Brock
@Psytronic: but the fact that you forgot the \r is my illustration of why it makes sense to use CGI.pm.
reinierpost
@Gavin: the speed hit won't be so bad unless you're using standalone CGI scripts.
reinierpost
+2  A: 

Strictly speaking, you have to print CRLF pairs. That is not the same thing as "\r\n\r\n" unless you use binmode STDOUT first.

print "Content-Type: text/html\r\n\r\n";
C:\Temp> t | xxd
0000000: 436f 6e74 656e 742d 5479 7065 3a20 7465  Content-Type: te
0000010: 7874 2f68 746d 6c0d 0d0a 0d0d 0a         xt/html......

You should use CGI.pm. Or, if, like me, you do not want all the historical baggage, use CGI::Simple.

Sinan Ünür