CMPS 335 Advanced Web Publishing
Perl and CGI Programming
Introduction to CGI and Perl
HTML is a markup language that can be used to create only static
Web pages to display information. HTML does not have facilities for
user interaction, data processing, or access to a database. One of
the approaches to adding these abilities is to execute programs on the Web
server. With CGI (Common Gateway Interface) and Perl, we can create
interactive Web applications that are more exciting and useful.
What is CGI?
- CGI is a set of rules for the communication between a
browser and a server for running programs to process data, perform
computation, or access a database. CGI is a protocol, not a
programming language.
- A CGI program can be written in any programming language
that follows the CGI protocol. Perl is the most popular CGI
programming language. Some CGI programs are not Perl programs.
- A CGI program is stored and executed on a Web server. A
Web page can call and run a CGI program and the CGI program can return
results to the browser for display on a Web page.
- A CGI program (usually called CGI script) can access data obtained from
a user on a Web page, process the data, and return results to the user.
- CGI experince is in high demand from employers. You could
substantially improve your salary and job prospects by learning CGI.
What is Perl?
- Perl is currently the most popular language for Web CGI programming.
It is the de facto programming language for dynamic and interactive
web pages.
- Perl stands for Practical Extraction and Report Language. It
was created by Larry Wall in 1986.
- Perl is an interpreted language. This means that there is no
compilation process to convert the source file to an executable
file. Perl statements are directly interpreted and executed one
at a time.
- Perl 5 is a powerful, general purpose programming language.
Some Perl scripts are not CGI scripts.
- Perl is particularly suited to Web-related tasks. It is a
powerful text-manupulation tool and it is cross-platform, easily ported
from one platform to another.
- Perl and HTML work harmoniously together. HTML code can create
links and forms that activate Perl scripts. Perl scripts can
generate Web pages to display the results produced by the Perl scripts.
- We can write Perl scripts for processing forms, creating guestbooks,
counting the number of times a Web page is visited, and much more.
Web Servers
- Two most popular Web servers are Apache in UNIX systems and
IIS (Internet Information System) in the Mocrosoft world.
- Unix operating system is one of the most powerful and flexible operating
systems. Most people have their Web pages stored on Unix Web
servers. We will forcus on using Perl with Unix systems.
- Name of the Unix Web server used in this class:
cs.selu.edu
Address of the Perl interpreter: /usr/bin/perl
Perl Scripts
- HTML files must be stored either in the public_html directory or
in a subdirectory inside the public_html directory and must have
world-readable permissions (file permissions 644). Perl cgi
scripts should be in the cgi-bin subdirectory inside the public_html
folder and have file permissions 755.
- You can create or edit a Perl script using either one of the
following methods:
- Create or edit a script locally on your PC and use FTP to
upload the script to the Web server. (Using Notepad
text editior)
- Use Telnet or Secure Shell client to logon to the Web server
and directly create or edit the script on the server. (Using
Pico text editior)
Save cgi scripts to the cgi-bin directory with the .cgi filename
extension. This filename extension makes the scripts easily
identifiable to both you and the server.
- When uploading cgi scripts, you must use the ascii or text
mode to send the scripts. Do not use the binary mode.
- A Perl script must start with the following statement:
#!/usr/bin/perl
This line must be the first in the script and begin in column 1.
There must not contain even a space line before this statement.
- Perl statements must end with a semicolon, except the opening
and closing lines of loops and conditional blocks.
- Perl code is case-sensitive, as are Unix commands and filenames.
- A Perl CGI script that generates output to the browser must include
the following all-important MIME (Multipurpose Internet Mail
Extensions) Content-type line that the browsers require:
print "Content-type:text/html\n\n";
Creating or Editing a Perl Script
Example: $ pico example1a.cgi
Displaying Acess Permissions of a Perl
Script
Example: $ ls -l example1a.cgi
Changing Permissions of a Perl Script
Example: $ chmod 755 example1a.cgi
Checking Syntax Erros of a Perl Script
Example: $ perl -c example1a.cgi
Running a Perl non-CGI Script (in the
Unix shell)
Example: $ perl example1a.cgi
Running a Perl CGI Script
(a) Running the Perl script in the Unix shell
Example: $ perl example2a.cgi
(b) Running the Perl script in the browser's
window
Example:
http://cs.selu.edu/~jhu/cgi-bin/example2a.cgi (URL in the Address box)
The print Function
The print function is essential to Perl CGI scripts.
It is the key to displaying the information gathered or processed by the
scripts. The print can display data the monitor screen or save data
to a file. The most frequently used print escape sequence is \n.
A print list can be enclosed by either double quotes or
single quotes. When double quotes are used, the list is printed
with variable interpolation. When single quotes are used, the
list is printed exactly as it apears without variable interpolation.
The heredoc Operator (<<)
You can use the heredoc operator to save you time of typing,
without using multiple print statements and \n escape sequences.
The syntax of heredoc is:
print <<"heredoc-marker"; or print <<heredoc-marker;
.
.
heredoc-marker
where heredoc-marker is a word or a phrase. The heredoc
marker is a label to signal the end of the print statement.
Note:
a. There should be no spaces between the heredoc operator << and
the heredoc marker.
b. The ending heredoc-marker should start in column 1 and should contain
nothing else, not even a trailing space or semicolon.
c. You can enclose the heredoc-marker in single quotation marks so that
there is no variable interpolation.
Simple Perl non-CGI Scripts
(A) Example 1a (Without using the heredoc operator)
[jhu@cs cgi-bin]$
[jhu@cs cgi-bin]$ chmod 755 example1a.cgi
[jhu@cs cgi-bin]$ ls -la example1a.cgi
-rwxr-xr-x 1 jhu jhu 363 Aug 13 16:00 example1.cgi
[jhu@cs cgi-bin]$ perl -c example1a.cgi
example1.cgi syntax OK
[jhu@cs cgi-bin]$ cat example1a.cgi
#!/usr/bin/perl
# --------------------------------------------------------
# Perl script to run in the UNIX command shell
# (Without using the heredoc operator)
# --------------------------------------------------------
# This program is not a CGI. It is a simple Perl script.
#
$you = "Janice";
print "Hello, $you.\n";
print "Welcome to Perl CGI Programming.\n\n";
print 'Hello, $you.\n';
[jhu@cs cgi-bin]$ perl example1a.cgi
Hello, Janice.
Welcome to Perl CGI Programming.
Hello, $you.\n
(B) Example 1b (Using the heredoc operator)
[jhu@cs cgi-bin]$ perl -c example1b.cgi
example1b.cgi syntax OK
[jhu@cs cgi-bin]$ cat example1b.cgi
#!/usr/bin/perl
# --------------------------------------------------------
# Perl script to run in the UNIX shell
# (Using the heredco operator <<)
# --------------------------------------------------------
#
$you = "Janice";
print <<"End1";
Hello, $you.
Welcome to Perl CGI Programming.
End1
print <<'End2';
Hello, $you.
Welcome to Perl CGI Programming.
End2
[jhu@cs cgi-bin]$ perl example1a.cgi
Hello, Janice.
Welcome to Perl CGI Programming.
Hello, $you.\n
Simple Perl CGI Scripts
(A) Example 2a (Without using the heredoc operator)
[jhu@cs cgi-bin]$ chmod 755 example2a.cgi
[jhu@cs cgi-bin]$ perl -c example2a.cgi
example1.cgi syntax OK
[jhu@cs cgi-bin]$ cat example2a.cgi
#!/usr/bin/perl
print "Content-type:text/html\n\n";
#
#-------------------------------------------------------------
# Perl CGI script that creates output for a browser
# (Without using the heredoc operator)
#-------------------------------------------------------------
# Perl does not interpret HTML tags
# Browser does not interpret \n (Perl print escape sequence)
#
$you = "Janice";
print "<html>\n<head><title>Example 2a</title></head>\n";
print "<body>\n<br><br>";
print "<font size=4><b>Hello, $you.</b>
<br>Welcome to Perl CGI Programming.</font>\n";
print </body>\n</html>\n";
Running the script in the Unix command shell
jhu@cs cgi-bin]$ perl example2a.cgi
<html>
<head><title>Example 2a</title></head>
<body>
<br><br><font size=4><b>Hello, Janice.</b>
<br>Welcome to Perl CGI Programming.</font>
</body>
</html>
Running the script (example2a.cgi) in the browser's window
(Enter the address: http://cs.selu.edu/~jhu/cgi-bin/example2a.cgi
and the following output appears in the browser's window)
Hello, Janice.
Welcome to Perl CGI Programming.
(B) Example 2b (Using the heredoc operator)
[jhu@cs cgi-bin]$ chmod 755 example2b.cgi
[jhu@cs cgi-bin]$ perl -c example2b.cgi
example2b.cgi syntax OK
[jhu@cs cgi-bin]$ cat example2b.cgi
#!/usr/bin/perl
print "Content-type:text/html\n\n";
#
# -------------------------------------------------------
# Perl CGI script that creates output for a browser
# (Using the heredoc operator)
# -------------------------------------------------------
#
$you = "Janice";
print <<End
<html>
<head><title>Example 2b</title>
</head>
<body>
<br><br><font size=4><b>Hello, $you.</b>
<br>Welcome to Perl CGI Programming.</font>
</body>
</html>
End
Running the script in the Unix shell
[jhu@cs cgi-bin]$ perl example2b.cgi
Content-type:text/html
<html>
<head><title>Example 2b</title>
</head>
<body>
<br><br><font size=4><b>Hello, Janice.</b>
<br>Welcome to Perl CGI Programming.</font>
</body>
</html>
Running the script in the browser's window
(The following output appears in the browser's window)
Hello, Janice.
Welcome to Perl CGI Programming.
Example of An FTP Session
C:\> ftp cs.selu.edu
User: (your user name)
Password: (Your password)
ftp> cd public_html/cgi-bin
ftp> ascii
ftp> send a:\counter.cgi
ftp> send a:\count.dat
ftp> cd ../
ftp> bin
ftp> send a:\butterfly.gif
ftp> quit
C:\> exit
Click Here for Unix commands
Also see Appdendices B and D for more on file permissions
and Unix commands
Click Here for more on Pico
Return to CMPS 335 Home Page
Return to Web Site Home Page