A
basic CGI Counter
(how to read and write to a text file)
by pixelatedcyberdust, 1/29/04
Follow along with the comment tags so you understand the script.
-------------------------------
#!/usr/bin/perl
#This is the location to perl itself on your server
use warnings;
use strict;
# Warnings and Strict are both security modules to help protect your
script. They will
# warn you if it 'thinks' you're doing something you didn't mean to or
it'll flat out
# die and hopefully give you a reason or an idea on what's wrong.
my $file = "counter.txt";
# Since we're using Strict, we have to globalize the variable with 'my'.
All new non-special
# system variables will have to be my'd. This is the location of the text
file you want to
# read and write to. It can have folder names such as /cgi-bin/counter.txt
but be sure the
# file already exists otherwise Warnings will kick in.
use CGI qw/:standard/;
# Since this is a web script (one to be used and run on a web server),
we're going to need to
# use the CGI module. This module makes it easier for us to print HTML to
a web page.
open( LOG, "$file" ) or die "Cannot open file $!";
# we now begin by opening a file for read-only. We open it, give it a
filehandle then give it
# a file location. LOG is our filehandle, but in reality it can be any
word you want: COUNTER,
# NEW or even DOG, but it's best to keep descriptive filenames.
# The location of $file near the beginning of the script. If for some
reason the file cannot
# be found or opened, "or die" will kick in, crash the script and give
you a reason.
my $cnt = <LOG>;
# Now that LOG is opened, we have to save its contents to a variable.
Your variable name
# must be assigned to the same 'filehandle' as you called it when you
opened it on the
# previous line.
close(LOG);
# The data is stored in a variable so we no longer have a need to read
the file. Close it
# by closing it's filehandle.
++$cnt;
# $cnt holds whatever was contained in $file, hopefully it was a number
so we can increment it.
# We are pre-incrementing (++ before the variable) so it calculates its
new value before you print it
# back to the LOG file. ++ will increase the number by 1.
open( LOG, "> $file" );
# We now have to reopen the file, this time for WRITE only. We do this by
adding ">" before the $file
# location. We are using the same LOG filehandle for simplicity.
print LOG $cnt;
# Printing to a file or database is a little different than printing to
the screen. For the $cnt
# to be written to our text file, we have to print to the LOG filehandle.
We just printed the new
# value of $cnt to file.
close(LOG);
# Since we've written the data we needed, we close the file.
print "$cnt";
# If you pay close attention, you'll see we're not printing to a
filehandle. That means $cnt is going to
# be printed directly to your web page. A counter wouldn't do any good if
the visitor (or you) can't have
# a visual look at the counter. This prints the current count number to
your screen.
-------------------------------
To keep track of the count, you need to use SSI on your web page to initiate
this script. You can do so
using <!--#include file="counter.pl" --> in your site. Rename counter.pl to
point to the directory and
file of your new script.
Completed script:
-------------------------------
#!/usr/bin/perl
use warnings;
use strict;
use CGI qw/:standard/;
my $file = "counter.txt";
open( LOG, "$file" ) or die "Cannot open file $!";
my $cnt = <LOG>;
close(LOG);
++$cnt;
open( LOG, "> $file" ) or die "Cannot open file $!";
print LOG $cnt;
close(LOG);
print "$cnt";
------------------------------
|