1
#!C:\xampp\apache\bin\httpd.exe
$command=`perl -v`;
$title = "Perl Version";

print "Content-type: text/html\\n\\n";
print "<html><head><title>$title</title></head><body>";

print "
<h1>$title</h1>

\n";
print $command;

print "</body></html>";

I get this error:

Premature end of script headers: version.cgi

skaffman
  • 398,947
  • 96
  • 818
  • 769
eozzy
  • 66,048
  • 104
  • 272
  • 428

2 Answers2

6

You need to remove the extra backslash

This code:

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

Should be this:

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

EDIT

Also, the first line in the script looks wrong.

#!C:\xampp\apache\bin\httpd.exe

This should be the path to Perl, not httpd.

EDIT 2

Finally, this all would have been easier for you to solve if you added these two lines after the first line in your script:

 use strict;
 use warnings;

And run the script on the command line with the -c -w flags to compile-check and warnings-check your script, ie perl -cw yourscript.cgi. This will give you line numbers of errors and warnings in your script.

Altogether, your script could look like this:

#!C:\path\to\perl.exe

use strict;
use warnings;

my $command=$^V;
my $title = 'Perl Version';

print "Content-type: text/html\r\n\r\n";
print "
<html><head><title>$title</title></head><body>

<h1>$title</h1>

$command

</body></html>";
mrk
  • 4,999
  • 3
  • 27
  • 42
  • Also, strictly speaking, should be `Content-type: text/html\r\n\r\n` – mrk Jul 09 '11 at 17:16
  • No, that's a CGI header, not an HTTP header. You don't need to use the HTTP line endings because the web server takes care of that for you. – brian d foy Jul 09 '11 at 17:47
  • Minor nit: You can have string literals across multiple lines without the continuation `\` at the end of each line. They're not good for readability, and a heredoc should be used, but they do work. – unpythonic Jul 09 '11 at 17:52
  • Strings are allowed to span over multiple lines, and \ is only needed in Shell programming, not Perl. `q`/`qq` are not functions, but [operators](http://p3rl.org/op#Quote-and-Quote-like-Operators). – daxim Jul 09 '11 at 17:54
  • @Mark and @daxim - Wow, I was completely wrong about " and ' string literals spanning multiple lines...thank you! – mrk Jul 09 '11 at 18:02
  • Ah, so just the path to perl was incorrect. Worked! Thanks x10!! :) – eozzy Jul 09 '11 at 18:06
  • @daxim, Named operators (like `qq` and `print`) are often called functions. – ikegami Jul 10 '11 at 03:13
2

You have written \\n where it should be \n in the header.

ETA: Also, perl -v is not a very good way to get the version. The variable $^V contains a more succinct and specific version number.

TLP
  • 66,756
  • 10
  • 92
  • 149