spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / web / programming / perl / 101 / variables

The Variables

Developer News
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next

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 http://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 (http://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



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Choosing the Right Online Backup Provider · Mother Avaya Nurtures Her Technology Partners · Software as a Service a Winning Model for Hotspot Provider


Revised: May 8, 1998

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