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

Thursday, February 3, 2011

Perl Code Examples

Here's some code I just wrote:


#! /usr/bin/perl -w

undef $/ ;

my $email = <ARGV> ;

if ($email =~ m|(bananas)|is)
{
print $1, "\n" ;
}

Here are some of the things going
on here:

  • The scripts slurps the entire
    file into one string
  • The name of the string
    it puts everything into is called
    $email
  • The file that is being
    slurped in is named on the
    command line

I use this script to test whether or not a
pattern I'm thinking of using is going to
work with Spamassassin. Spamassassin
filters email for spam.

Let's say, for example, there were a drug
that was being promoted through spam
email called longify. I'm hoping
there is no such drug. I'm trying to come
up with a name that is purely hypothetical.

Let further say that the words last longer
with longify
have appeared in a spam
email. I might test this pattern by doing the
following:

#! /usr/bin/perl -w

undef $/ ;

my $email = <ARGV> ;

if ($email =~ m|(longify)|is)
{
print $1, "\n" ;
}

I've altered the above script so that
any text file I place on the command
line will be scanned for the word
longify.

Let's say I save my spam email as a
text file called spam.txt. Let's
further say that I call the above script
checkspam. This being the
case, I will invoke my script as follows:

$ checkspam spam.txt

If the script replies with the word
longify, the script found
the pattern. If it is silent, it did not.

Ed Abbott