Perl CGI scripts - how to get started, tutorials, resources, simple examples.

CGI Scripts

HomeBuildingPromotionIncomeAdvancedToolsResources

How CGI scripts work

There are a lot of things you can do with your website using HTML: display beautiful pages, show graphics, create links to other pages. But plain HTML documents are static, which means they are text files that don't change. CGI scripts, on the other hand, are executed in real time so that they can output dynamic information.

CGI, which stands for Common Gateway Interface, is not a programming language but a protocol - a set of rules for how a Web server talks to a program. CGI scripts allow you to add interactive and dynamic content to your Web pages. You can display a page counter, keep track of your visitors or have your own chat room to let your users interact.

Many Web servers let you run CGI scripts written in a scripting language called Perl because it's well suited for that kind of thing. Perl is an interpretive language, so the Perl scripts you use don't have to be compiled. You just copy the Perl CGI scripts onto the right part of your server, and they're ready to go.

Resources on Perl CGI scripts

Beginning Perl, Second Edition
by James Lee

It's a very capable introduction to Perl. The flow of the book is logical, straightforward, and highly readable. However, it's intended for reasonably experienced programmers. It's not intended to teach Perl as a first computer language.

 

Writing CGI Applications with Perl
by Kevin Meltzer, Brent Michalski

This book by two longtime Perl programmers shows how the Perl scripts can accomplish the tasks needed for many online applications. Each chapter covers a specific Web-based application and explains the code line by line (or block by block).


You may also find it helpful to browse these online CGI tutorials...

CGI Tutorial for the Total Non-Programmer
This tutorial will walk you through the basics of creating Perl CGI scripts on Unix platform. At the end of this tutorial you'll be able to create Perl scripts for counting hits, storing and retrieving guestbook information, and processing secure passwords.

Beginner's Guide to CGI Scripting with Perl
This page is designed to help novice programmers learn the Perl programming language. Specifically, it's designed to help them learn enough to run Perl scripts on a Unix Web server.

CGI Programming FAQ
This CGI tutorial will give you answers to the most common questions from the role and nature of CGI scripts to programming hints and tips for a number of popular tasks.

Simple Perl CGI script

Here's the source code of a simple Perl script that places one item from a set of items (tips, thoughts, quotes, banners) on your Web page. It simply includes in your page one full line from a big text file that is uploaded to your server in conjunction with the script. Since the lines may be long and may contain any HTML tags, you can easily create, for example, banner rotation.


The actual Perl lines are in red.
The explanations to you are in black.


#!/usr/bin/perl
The first line of the CGI script shows the Perl path on your server (usually #!/usr/bin/perl or #!/usr/local/bin/perl).
Always put this line at the top of your Perl scripts.

#------------------------------
# tip.pl - simple Perl CGI script
#------------------------------
Any line beginning with a "#" mark is a comment.

$fn = '/home/UserName/public_html/cgi-bin/data/tips.cnt';
The variable $fn contains the path to the count file tips.cnt within your server. Check your ISP documentation to find the correct path to your cgi-bin directory. The subdirectory data is created specially for CGI output.

$i = 0;
The variable $i will contain the current count value.

if( -e  $fn ) {
If the count file exists, then...

   open( DF, $fn );
Opens the count file for reading and associates it with the file handle DF. In Perl CGI scripts, file handles are always written in uppercase so they can be easily identified.

   $i = <DF>;
Reads the current count value into the variable $i.

   close( DF );
Closes the file.

}
If the count file wasn't found, the variable $i remains constant (i.e. $i=0).

$i++;
Increments $i.

open( DF, ">$fn" );
Opens the count file for writing (">" before the file name). If a file by that name already exists, it'll be overwritten.

print DF $i;
Writes the incremented value to the file.

close( DF );
Closes the file.

open( DF, '/home/UserName/public_html/cgi-bin/tips.txt' );
Opens the data file tips.txt for reading.

$n = <DF>;
Reads the first line of the data file into the variable $n. This line contains the number of informational lines in the file.

$n = $i % $n;
Calculates the current count value within the data file as remainder of (count divided by number of lines).

print "Content-type: text/html\n\n";
Outputs the standard header for HTML documents. The visitors won't see it, but the server and browser will, and that's the important thing in CGI scripts.

for( $i=0; <DF>; $i++) {
Starts the for loop that reads the next line into the default variable and increments the variable $i, so that it contains the number of this line.

   if( $i == $n ) { print; last; }
If the line number $i equals the count value, prints the default variable and breaks out of the loop.

}
close( DF );

Closes the open data file.


To use this Perl script, just do the following...

1) Create the file tip.pl with the above program in your text editor. Be sure to include the correct paths to Perl, the count file tips.cnt, and the data file tips.txt. Check your ISP documentation for this.

2) Create the file tips.txt with your items, each item on a separate line, without blank lines between them. The first line must contain the number of items. For example...

3
Write Perl CGI Scripts for Fun and Profit!
Hidden DOS secret: add BUGS=OFF to your config.sys.
Press any key to continue or any other key to quit...

3) Log into your FTP.

4) Inside your cgi-bin directory, make a directory called data and chmod it 777.

5) Upload tips.txt and tip.pl into your cgi-bin directory, being careful to upload as ASCII only. Chmod the CGI script tip.pl 755.

6) Call the Perl script from your page(s)...

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

Notes:

  • Save these HTML pages with the extension ".shtml" (to enable SSI parsing of files).
  • To force the browser to load a new copy of the page from the server each time the page is viewed (useful for pages with dynamic content), use Meta Expires tag...
       <meta http-equiv="expires" content="0">

For more information on uploading and chmoding files, see Building: Web Hosting: FTP Software.

Fast and Reliable - cPanel Hosting

What's New

Inexpensive Web Hosting
How to choose a fast and reliable service from the bulk of cheap hosting solutions.

 

Easy Website builders
Easy way to build a professional looking site for commercial use or just for fun.

 

How to create a mobile version of your website

 

Multiple domain hosting
A low-cost solution for owners of multiple Web sites.

Sponsored links

Copyright © 2002-2017 BuildWebSite4u.com