Adding an Access Counter
A text access counter
The Increment subroutine
The complete text access counter script
Seeding the Counter
Adding the Counter to Your HTML Page
With Server Side Includes
Without Server Side Includes
Creating the Template File
Modifying the access.pl Script
A graphical access counter
Incrementing the Counter
Creating the GIF Image
Returning the Graphical Counter Image
The Graphical Counter Script
Calling the Counter with the <IMG> Tag

[Previous] [Next]


The Increment subroutine


Your text access counter first needs to read in the current access count, which is stored in the counter file. In this article, the text file containing the access count is named count.dat. You can read in the value stored in count.dat simply by opening the file and reading the first line into the variable $count,, as in the following lines of Perl code:
 open(COUNT, "$file") || die "Content-type: text/html\n\nCannot open counter file!";
 $count = <COUNT>;
 close(COUNT);


The first line opens the file whose name is stored in the variable $file. You set this variable when you put together the entire text access counter script shown in Listing 2. For now, just note that $file will contain the path and name of the file that stores the current number of accesses to your Web page. The second part of the first line of Perl code contains a die statement that will terminate the program and output the contents of the string:
 Content-type: text/html\n\nCannot open counter file!


The || operator between the open and die statements is the logical OR operator. When this operator is between the two statements, the Perl interpreter first tries to execute the open statement. If the open is successful, the Perl interpreter moves on to the next line of code. However, if the file cannot be opened, the Perl interpreter executes the die statement. This is a common way to verify that a file is successfully opened and to terminate the Perl program if it is not.

The second line of code reads in the contents of the first line of the counter file from the input stream <COUNT> and places it into the variable $count. After the line has been read in from the file, you can close the input stream <COUNT> by using the close command, as in the third line of code.

Now that you have the code to get the current value of the access counter, you need to increment the value and write the new value to the counter file. You can easily increment the value by using the ++ operator. If you append this operator to a variable name, the integer value stored in the variable is increased by 1. For example, if the current value of the access counter were 2, the Perl code

 $count++;


would change the value to 3.

Once the access count has been incremented, you need to store the new value in the counter file for the next time the script is called. The following three lines of Perl code open the counter file and write the new access count to the file:

 open(COUNT, ">$file") || die "Content-type: text/html\n\nCannot open counter file!";
 print COUNT $count;
 close(COUNT);


Again, you use the open statement to open the counter file. However, this time you output to the file instead of receiving input from the file. Notice the > operator before the $file variable in the open statement. This operator opens the file for output. If the file already exists, it is overwritten. The second line prints the contents of the $count variable into the file.

You now have all the code necessary to read in the current access count, increment it, and write it back to the file. To make your code easier to read, place it within a subroutine called Increment. Listing 1 shows the contents of the Increment subroutine.

Listing 1: The Increment Subroutine
sub Increment {
   local ($count);

   # Get the current value of the access counter.
   open(COUNT, "$file") || die "Content-type: text/html\n\nCannot open
 counter file!";
   $count = <COUNT>;
   close(COUNT);

   # Increment the access counter

   $count++;

   # Store the value of the counter in the counter file.
   open(COUNT, ">$file") || die "Content-type: text/html\n\nCannot open
 counter file!";
   print COUNT $count;
   close(COUNT);

   return $count;
 }





Besides the sub Increment line, which declares the subroutine, the only lines of code that have been added are the local statement at the beginning and the return statement at the end. The local statement,
 local ($count);


declares that the $count variable is local only to the Increment subroutine. A local variable is a variable that exists only within a portion of your Perl code, usually within a subroutine. If a variable with the same name existed outside the subroutine, Perl would consider it a different variable than the one that is declared local within the subroutine. Declaring your subroutine's variables as local helps to keep your subroutines from overwriting values of global variables. A global variable is one that is accessible throughout the entire Perl program, including any subroutines in the same Perl file. In Listing 1, the variable $file is a global variable. Listing 2 in the next section will show where this variable is set.

The other line of Perl code added to your Increment subroutine is the return statement

 return $count;


This statement causes the subroutine to return the current value of the count as its return value. In Perl, every subroutine returns a value. You can set this return value explicitly by using the return statement. The next section demonstrates how this return value is used.


[Previous] [Next]