Friday, July 26, 2013

Perl Hello World on a Web Server

I went looking for a simple script this morning to check to see if Perl was up and running on a web server. Here's one that does just that:

Hello World CGI,
How to Create a Simple Perl CGI

I modified the script slightly:


#!/usr/bin/perl -w

use strict;
 
print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>Test Perl on Webserver</title>
</head>
<body>
<h1>Test Perl on Webserver</h1>
<p>Hello World</p>
</body>
</html>
HTML

Whereas the above website, has an exit statement, I chose to leave it off since Perl falls off after the last statement anyhow. Therefore, having an exit statement has no distinguishing effect.

The other thing I did was insert the closing HTML tag. Their Perl script has no closing tag, undoubtedly an oversight.

I put the closing HTML tag in as a formality, more than anything else. Once closing tag that is missing is not going to make any difference.

Here's how I might rewrite the script to make it more comprehensible to beginners:


#!/usr/bin/perl -w

use strict;
 
print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>Test Perl on Webserver</title>\n";
print "</head>\n";
print "<body>\n";
print "<h1>Test Perl on Webserver</h1>\n";
print "<p>Hello World</p>\n";
print "</body>\n";
print "</html>\n";

Again, the effect is the same. However, with my rewrite I think it might be clearer that each line of HTML is being printed by Perl. Furthermore, the HTTP header is the first thing that is printed.

Here's a link that look's useful:

Troubleshooting Perl CGI scripts

Good luck getting your CGI script working! CGI is one of the older web technologies. It has been around since the 1990's. You will find lots of information on CGI on the web.

Ed Abbott

No comments:

Post a Comment