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.
# 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";
And your quote is:
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.