Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

© 2004
pages: 1420

Amazon logo

This tutorial shows how to use Perl server-side includes (SSIs) to add dynamic content to a Web page. In particular, we demonstrate a Web page hit counter. The tutorial is intended for students and professionals who are already familiar with Perl programming.
Download the Code Example
[Note: This tutorial is an excerpt (Section 25.6) of Chapter 25, Perl, from our textbook Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, 2004, pp.866-870. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
25.6 Server-Side Includes (Continued)
Line 9 uses the diamond operator, <>, to read one line of the file referred to by filehandle COUNTREAD and assign it to the variable $data. When the diamond operator is used in a scalar context, only one line is read. If assigned to an array, each line from the file is assigned to a successive element of the array. Because the file counter.dat contains only one line (in this case, only one number), the variable $data is assigned the value of that number in line 9. Line 10 then increments $data by 1. If the file does not yet exist when we try to open it, $data is assigned the value undef, which will be evaluated as 0 and incremented to 1 in line 10. In line 11, the connection to counter.dat is terminated by calling function close.
Now that the counter has been incremented for this hit, we write it back to the counter.dat file. In line 13, we open the counter.dat file for writing by preceding the file name with a > character (this is called write mode). This immediately truncates (i.e., discards) any data in that file. If the file does not exist, Perl creates a new one with the specified name. The first argument (COUNTWRITE) specifies the filehandle that will be used to refer to the file. Perl also provides an append mode (>>) for appending to the end of a file.
After line 13 executes, data can be written to the file counter.dat. Line 14 writes the counter number back to the file counter.dat. The first argument to print indicates the filehandle that refers to the file where data is written. If no filehandle is specified, print writes to standard out (STDOUT). Note that we need to use a space, rather than a comma, to separate the filehandle from the data. Line 15 closes the connect to the file counter.dat.
Lines 21-24 use a for statement to iterate through each digit of the number scalar $data. The for statement syntax consists of three semicolon-separated statements in parentheses, followed by a body delimited by curly braces. In our example, we iterate until $count is equal to length( $data ). Function length returns the length of a character string, so the for iterates once for each digit in the variable $data. For instance, if $data stores the value "32", then the for iterates twice, first to process the value "3", and second to process the value "2". In the first iteration, $count equals 0 (as initialized in line 21), and the second time $count will equal 1. This is because the value of $count will be incremented for each loop (as specified by the statement $count++, also in line 21). For each iteration, we obtain the current digit by calling function substr. The first parameter passed to function substr specifies the string from which to obtain a substring. The second parameter specifies the offset, in characters, from the beginning of the string, so an offset of 0 returns the first character, 1 returns the second and so forth. The third argument specifies the length of the substring to be obtained (one character in this case). The for then assigns each digit (possibly from a multiple-digit number) to the scalar variable $number. Each digit's corresponding image is displayed using the img function (line 23).
Good Programming Practice 25.1
 25.1 When opening a text file to read its contents, open the file in read-only mode. Opening the file in other modes increases the risk of overwriting the data accidentally.

Good Programming Practice 25.2
 25.2 Always close files as soon as you are finished with them.
It is important in this example to think about file permissions and security. This program may not run correctly if the user's default settings do not allow scripts to manipulate files. To resolve this issue, the user can change the permissions in the folder where counter.dat resides so that all users have Write access. Check your Web server's documentation for instructions on configuring file permissions. The user should be aware that giving users Write access poses a security risk to the system. Security details are covered in Chapter 38, e-Business & e-Commerce.
Page 1 | 2 | 3 | 4
Return to Tutorial Index