Perl 101: The Variables | WebReference

Perl 101: The Variables

The Variables

Perl 101

Now that you know you can upload a script and execute it successfully, it is time to make some more changes to the script so we can increase its functionality. Let’s open the program in the text editor and move onto the next few lines:

######### This Section is where we will START to define all of our variables #########

# The name of our text file we are going to use
$FileName = "";

# The path (not the url!) to the directory where the text file is located
$ServerPath = "";

######### This is where we will END identifying our variables ####################

The first two and final lines of this block of code are comments. It is always a good idea when writing a program to include comments to help you better understand what you have written. The first new thing we encounter is a variable. The best way to think of a variable is as a place to store something. For right now, we are going to use scalar variables, or variables that can only hold one piece of information. The first variable we encounter, $FileName, will hold the name of our file where we wish to store our information. We can name our file whatever we want to. I chose to name it information.txt, but conceivably it could be named anything: sam.data, file.test, etc.. We edit our variable by inserting the information between the empty quotation marks. Our line would now look like this:

$FileName = "information.txt";

It is very important that you remember to terminate all of your lines with a semicolon. There are a few exceptions to this rule but it is best to operate under the notion that all simple expressions in Perl are terminated with a semicolon. The next scalar variable we need to modify is the $ServerPath variable, or the path to our text file. When working with Perl it is important to remember the difference between server paths and URLs. An URL would look like this https://www.somedomain.com, while a server path would look like this "/usr/www/web/". A server path differs in that it tells the server how to access the file as opposed to how the client or user would access the file. The variable must end with the appropriate Perl directory delimiter, "/" for Unix and Windows or ":" for the MacOS. You should again consult your ISP to determine the correct server path to your root directory. Let’s say for example that the correct server path to your root directory is "/usr/www/web/". If we wanted to access a folder in your root directory named info,we would notate the server path as "/usr/www/web/info/". As we configure the server path variable, you will need to decide where you want to place your text file. It can be in any directory on your server that you have access to. For this example, we will assume that you are placing it in a folder named info and that the correct server path would look like this:

$ServerPath = "/usr/www/web/info/";

The next thing we need to do is to create an empty file with the corresponding name you specified in the $FileName scalar variable. Once this has been completed you are ready to upload the program and file to the server. Make sure when you upload the file, information.txt--stored in our variable $FileName--that you upload it to the directory you specified in $ServerPath. After you have uploaded the file you will need to set its permissions to 777, chmod 777 information.txt. In some circumstances, you may also have to chmod the directory as well

Now that you have everything configured properly and uploaded to the server, it is time to test to see if you’ve made any mistakes. First let’s start out by just calling the script by itself (https://www.yourdomain.com/cgi-bin/script.cgi). If it says "you have been logged" on the screen then you have installed the script correctly. Now we need to call the script the way it was meant to be called, via a server side include. Generally all you need to do to use a server side include is to change the extension of the file from html or htm to shtml (somefile.htm becomes somfile.shtml). You then need to insert a call to your program within the html that would look like this:

<!--#exec cgi="/cgi-bin/script.cgi" -->

This tells the server to execute the script whenever the page is requested. So let’s just refresh the page a couple of time after it has fully loaded to log ourselves in. Now if we FTP the text file back down again and open it up we should see something like this:

05/01/98|03: 22 AM||12.79.6.169|Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)|cgi_test.shtml
05/01/98|03: 22 AM||12.79.6.169|Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)|cgi_test.shtml
05/01/98|03: 22 AM||12.79.6.169|Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)|cgi_test.shtml

You will notice that, every time you called the program, it appended a line to the file with some information about your visit. The information is being collected and stored in a flat file database. We are collecting and separating information with a delimiter, in this case a pipe symbol (|). We could have used any character(s) we want as a delimiter as long as we know it will not appear in the data. Our information looks like this:

Date|Time|Referring Page|Your IP Address|Your Browser|Page being called from

The advantage of only putting one visit per line and separating the information with a pipe character is that it will be easier to decode the information if we standardize the way it is collected and stored.

Comments are welcome


Revised: May 8, 1998

URL: https://webreference.com/programming/perl/101/variables.html