Listing 1: frontpage.pl

This section analyzes the key parts of the sample source code for generating the front page. For a full listing of this source code, see frontpage.pl, basic version.

First, we load some Perl modules, and print out a standard HTTP header (along with a Cache-Control:private header—see the next section about why we do this).

#!D:\Perl\bin\perl.exe 
use CGI qw(:standard); 
use CGI::Cookie; print header(-'Cache-Control'=>'private');

Next, we retrieve any cookies associated with this page.

%cookies = fetch CGI::Cookie;

If we find a cookie marked moviereviewID, then we open our database, look for a user with that ID number, grab his first name into the variable propername and his movie preferences into an array of 11 true-or-false values called prefs. (Again, remember that all my values are stored in the database as one large string with different fields separated by ~:~ characters. If I were using a more sophisticated database, I could go with something more elegant.)

if ($cookies{'moviereviewID'} ) {
# If a cookie exists, we get its value 
# (which is the user's ID number)

$myID = $cookies{'moviereviewID'} -> value; 

dbmopen(%prefs,"movieuserprefs",0644) 
    || die "Can't open movieuserprefs! $!";
# We look up the user's preferences based on 
# his ID within the database. 
# 
# The record itself is a big string of values
# separated by ~:~ characters. The "split" 
# function will break up that string and place 
# the resulting values into the variable 
# "propername" and the array "prefs"
($propername, @prefs) = split(/~:~/, $prefs{$myID});

dbmclose(%prefs);

} else {

If such a cookie does not exist, we load up some default values and note that we are just loading default values.

# We make prefs an array of default values if 
    # the user's cookie does not exist. 

    @prefs = (1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0);

    # This is a flag that indicates we're using 
    # default values.

    $default = true; 
}

We print out some standard text for the beginning of the Web page.

print <<END_of_Start;
<HTML> <HEAD> 
<TITLE>Movie reviews</title> 
<META NAME="HandheldFriendly" content="True"> 
</HEAD> 
<BODY> 
<Center><h2>The movie review channel!</h2></center>
END_of_Start

print "<p>Welcome, $propername </p>" unless $default; 
print "<p>Here are a list of movies you might be 
interested in...</p>\n <ul>\n";

This is where the work really happens. We go through every movie and check the genres it fits into. If we find it belongs to one of the genres that our user has selected on his list, then we print a link to the movie with the URL of reviews/<movieIDnumber>.html.

dbmopen(%movies,"moviereviews",0644) 
    || die "Can't open moviereviews! $!"; 
# We iterate over each movie ID within the moviereviews 
# database. foreach $filmnumber (keys (%movies)) { 
# Once again, breaking up a string into the variable 
# "filmname" and the "genres" array by using a split 
# command.
($filmname, @genres) = split(/~:~/, $movies{$filmnumber}); 
$match = 0; 
for ($i =0; $i < 11; $i++) 
    { if ($genres[$i] && $prefs[$i]) 
    { $match = 1; # We've found a match! last; 
                  # Break out of the loop. 
                  # no need to keep looking. 
    } 
    }
# If this film has a match, include a link to it. 
If ($match) { 
    print "<li><a href=\"reviews/
           ${filmnumber}.html\"> 
           $filmname</a>\n"; 
    } 
} dbmclose(%movies);
print "</UL>\n<p>Tune in next time for more movie reviews!<p>\n";

We include a link to the personalization page, and some encouragement for users who have not set up their cookies yet.

If ($default) { print "<center>Want personalized content? Of
        course you do!\n"; 
    print "<br><a href=\"/cgi-bin/movies/editprefsform.pl\"> 
        Edit your preferences</a></center>\n"; } 
else { 
    print "<a href=\"/cgi-bin/movies/editprefsform.pl\"> 
        Edit preferences</a>\n"; } print "</body>\n</html>\n";

This will present a front page that looks a little something like this:

Default home page produced by frontpage.pl script