Now you have just enough information to be dangerous. Before we discuss some of your customization
possibilities, we'll explain briefly how the components you've already learned are put together to
form a perl program.
A program is, simply put, code that does something. A program would hardly be useful or fun if it
didn't do anything. In order for a program to do something it needs a few things. The most obvious is
the code to do the actions desired. The next are variables (data) to work with and the last is a
destination for the results. Even the smallest program needs these three things to perform any job.
The data can be results submitted by a feedback form (browser-based submission of data), an IP address
(server collected data), or text from any file that the program can open or obtain. The point is, the
program needs something to work with.
The output can be a file on the server, an email message, or even simply text results back to a browser.
A simple functional program
Putting together some of the elements we've previously discussed, let's look at a simple program. This
program will output the date, time, visitor IP# and a "Back" button which will remember the visitor's last
page. Though you can copy and paste the entire program from the text box below, you can download the
file here as well.
The above program has been included onto this page (using a Server Side Include) and yields the
following information based on your visit to this page:
[an error occurred while processing this directive]
Now, let's break down this code a bit and bring to your attention the various elements that came
together to make this program.
As we already know, the first line of a program needs to be the path to the perl interpreter on your server.
#!/usr/bin/perl
Now, there's a few lines of comments. Remember, these lines beginning with a pound sign
(#) are ignored by the program.
Next is one lonely variable -- $font, which is how you can
make the program output use the font face you want. Notice that the entire
<FONT FACE tag is enclosed in single quotes, so that double
quotes can be used without needing to escape the double quotes (If you wanted to enclose the font
tag in double quotes, this is what it would need to look like:
$font = "<FONT FACE=\"Verdana,sans serif\" SIZE=2>";).
$font = '<FONT FACE="Verdana,sans serif" SIZE=1>';
This next line probably doesn't look like it does much, but if you'll recall, this is one of the ways
that a subroutine is called. In this case, the date subroutine is called. The date subroutine will get
the server's date and time and format it for output.
&date;
The absolutely necessary Content-type line is next.
Without this line, the program will fail. Because we're using this output in an html page, we're
using the text/html definition.
print "Content-type: text/html\n\n";
Now, we're presenting some output. The font face is inserting using the
$font variable.
$ENV{'REMOTE_ADDR'} is an Environment Variable. This is
information that is readily available and provided by the server about your visitor. There are quite
a few environment variables (next month, we will have a more complete discussion on Environment Variables).
You may be wondering where $fulldate and
$time came from… we didn't define those variables. Both
$fulldate and $time
were assigned in the date subroutine. Since we called the
subroutine a couple of lines above, these two variables already have been assigned values.
You may also be wondering about the print qq~. Instead of
enclosing output in single or double quotes, there are a couple of other ways to enclose output.
I prefer qq~ because no quotes, single or double, need to
be escaped. When you're entering text into a program for output, be very careful with the format that's used.
Here's our first and only condition in the program. If there is a referring page, the program prints a
link back to it. If there's no referring page, then nothing will show. Then, we've included a close font
tag.
This line just tells the program that we're done - it can stop now.
exit;
This is our date subroutine, which gets the date and time from the server. You'll notice a couple
of arrays, and some more advanced-looking lines, which won't be discussed until the intermediate series.
You may be saying "Gotcha! That's not Y2K compliant!"
All I have to say about that is :Þ yes it is! Stay tuned… next month, a full article detailing
why the $year += 1900 is Y2K compliant,
and why we don't have to worry about compliance for another 30 years.
Though this is very simple program, I'm sure you can understand the concept of how the program elements
discussed in our "Intro" series come together to form a functional program.