Project I: Automating Articles
Throughout the projects in this book, we will list the files you will be using along with the permissions you will need to issue these files. For a description of how to change permissions on your files in Unix, please check out the brief tutorial on file permissions toward the end of Appendix B, "Miscellaneous Reference."
Files Used |
Permissions |
article1.html |
644 |
article2.html |
644 |
article3.html |
644 |
random.cgi |
755 |
readme.cgi |
755 |
Now let's see how to go about automating your articles with simple Perl scripts. Since you already have the header and footer information in the script, you can build your article HTML files without them. Otherwise, you will end up with duplicate header and footer tags.
Use the following three HTML files with their appropriate filenames (we will use these in the next few projects):
article1.html
<TITLE>Article 1</TITLE> <FONT SIZE="4"><B>Self protection</B></FONT><P> <FONT SIZE="3">How to fend off an attacker with your stiletto heel.</FONT>
article2.html
<TITLE>Article 2</TITLE> <FONT SIZE="4"><B>Salads!!!</B></FONT><P> <FONT SIZE="3">1001 ways to make salads.</FONT>
article3.html
<TITLE>Article 3</TITLE> <FONT SIZE="4"><B>Micro Minis are back</B></FONT><P> <FONT SIZE="3">Just how short is too short?</FONT>
Randomize
For our first project with the three HTML files you have just created, you will use the script random.cgi that will randomly display one of these files every time a user visits your Web page. Here is your first look at an array that is used to store the names of the files that will be randomly picked to be displayed. This is done by using the srand and rand functions.
NEW FEATURES
array
An array is another type of variable, but instead of holding a single piece of data, an array can hold several. A scalar variable begins with a $, whereas an array begins with a @. (An easy way to remember an array is by the a in the @ sign.) An array sorts these different fields by indexing them in order starting with 0. Note that it does not begin with 1 as you would think it would.
Syntax
name = ("Micah", "Chris", "Dan"); print "Array field 0 is: $name[0]\n"; print "Array field 1 is: $name[1]\n"; print "Array field 2 is: $name[2]\n";
Results
Array field 0 is: Micah
Array field 1 is: Chris
Array field 2 is: Dan
srand
The srand function initializes the random-number generator.
rand
The rand function takes as its argument an integer and then generates a random number between 0 and the integer.
Script 13random.cgi
#!/usr/bin/perl print "Content-type: text/html\n\n"; print "<HTML>\n<BODY BGCOLOR=\"#FFFFFF\">\n\n"; 1. srand; 2. @articles = ("article1.html", "article2.html", "article3.html"); 3. $random_number = rand(@articles); 4. $article = $articles[$random_number]; 5. open(FILE, $article); 6. while(<FILE>) { 7. print $_; } close(FILE); print "</BODY>\n"; print "</HTML>\n";
HOW THE SCRIPT WORKS
1. The srand function initializes the random-number generator.2. Now an array called @articles is created, and the three files article1.html, article2.html, and article3.html are added to it. If you would like to use more than these three files or other filenames, you can simply add them in place of these. You can have as many files as you wish.
3. This is where the rand function comes into play. The rand function takes as its argument an integer, which then generates a random number between 0 and the integer. This way, a random index is chosen from the @articles array. The value is then assigned to the variable $random_number.
4. Next, we assign the chosen filename associated with the index that was randomly selected from the @articles array and place it in a new variable called $article.
$article = $articles[$random_number];
5. open(FILE, $article); The file $article is now opened and assigned the FILEHANDLE FILE.
67. Now the script loops through all the lines of the file line by line while the FILE FILEHANDLE is open. While this is looping through the FILEHANDLE, during each iteration of the loop the current line of the FILEHANDLE is assigned to the special $_ variable (including the carriage return at the end). Simply printing out this variable is all that is needed. Through each iteration of the file, each line is printed out. This is the same thing that is done by the cat command in Unix, or the type command in MS-DOS/Windows.
Displaying Files by Time of Day
What if you want to display one of the three articles based on the time of day? For example, article1 from 12:01 a.m. to 8 a.m., article2 from 8:01 a.m. to 4 p.m., and article3 from 4:01 p.m. to midnight. This script will show you how by taking the $hour variable from a function called localtime, and then testing the variable with a series of if statements to determine which file to spit out to the browser.
NEW FUNCTIONS
if (statement)
The if statement tests whether a condition is true or not. If the statement is not true, it moves on.
elsif (statement)
Once the if statement has been executed, the elsif function is used to test if another condition is true.
else (statement)
else is used at the end of an if block statement to handle anything that wasn't in the previous conditionskind of like a catch-all statement.
You can use the if statement with several elsif statements, but only use the else statement one time, which will be at the end of the entire block of if statements. See Table 12 for some examples of how to and how not to use these statements.
TABLE 12 Uses of the Else Statements
Good |
|
Bad! |
if |
|
if |
(statement); |
|
(statement); |
elsif |
|
else |
(statement); |
|
(statement); |
elsif |
|
else |
(statement); |
|
(statement); |
else |
|
else |
(statement); |
|
(statement); |
localtime
The localtime function returns an array of nine elements of time in the following order:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
TABLE 13
Array Number |
Value |
0 |
Seconds |
1 |
Minutes |
2 |
Hour of the day (023) |
3 |
Day of the month |
4 |
Month (0 = January, 11 = December) |
5 |
Year (with 1900 subtracted from it) |
6 |
Day of the week (0 = Sunday, 6 = Saturday) |
7 |
Day of the year (0364) |
8 |
A flag indicating whether it is daylight savings time |
Script 14readme.cgi
#!/usr/bin/perl print "Content-type: text/html\n\n"; 1. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 2. if(($hour >= 0) && ($hour < 9)) { $article = "article1.html"; } 3. elsif(($hour > 8) && ($hour < 16)) { $article = "article2.html "; } 4. else { $article = "article3.html "; } 5. open(FILE, $article); 6. while(<FILE>) { 7. print $_; } close (FILE);
HOW THE SCRIPT WORKS
1. The localtime function returns an array of nine elements and assigns these values to their associated variables, $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst.
27. These next several lines use the if, elsif, and else functions to check to see if these variables meet certain criteria:
if(($hour >= 0) && ($hour < 9)): First, the script looks to see if the $hour variable is between the hours of midnight (0) and 8 a.m. (8) by seeing if it is equal to or greater than 0 and less than 9. && is the logical operator used for "and." If this is true, then the variable $article is assigned the value article1.html. If not, then the script moves on to the next statement.
elsif(($hour > 8) && ($hour < 16)): Now the script checks to see if the $hour variable is between 8 a.m. and 4 p.m. If this is true, then the variable $article is assigned the value article2.html. If not, then the script moves on to the next statement.
else: Finally, if none of the previous tests were true, then a final test is done, which is just an else (in this case it is a catch-all), and $article is assigned the value article3.html.