In the concluding article in the Perl 101 series, everythingyou've learnt so far is put to the test when you develop some real-worldCGI applications - a counter, a guest book and a form mailer.
Perl also allows you to run external commands, and display the output of those commands on your Web page. Consider the following simple fortune cookie generator, which uses the "fortune" program to give you a random quote each time you reload the page.
#!/usr/bin/perl
# fortune.cgi - get a random quote
# get a quote - change your path appropriately
$quote = `/usr/games/fortune`;
# print it in a page
print "Content-Type: text/html\n\n";
print <<EOF;
<html>
<head>
<basefont face=Arial>
</head>
<body>
And your quote is:
<br>
$quote
</body>
</html>
EOF
In this case, we've executed a command by enclosing it in
single quotes, and sent the output to the variable $quote. Next, we've used Perl to output an HTML page containing the quote - this page is the one you'll see when you visit the site through your browser. Each time you refresh it, you'll see a new quote.
Note the slightly different manner in which we've structured the print() statement here. The << marker indicates to Perl that what comes next is a multi-line block of text, and is to be printed as is right up to the marker (the marker in this case is the string "EOF"). In Perl-lingo, this is known as a "here document", and it comes in very handy when you need to output a chunk of HTML code.
This article copyright Melonfire 2000. All rights reserved.