CGI allows the web server to run other programs. These programs can be anything you can run at a command line while logged in. It can be a C or C++ program, a shell script, or a Perl script. Perl scripts are fairly common, because Perl is such a powerful programming language.
One advantage of using Perl is that you are running an interpreted script, instead of a compiled program. This allows you to make changes and run the script immediatly afterward. A perl script is a text file that is parsed and executed line by line by Perl. It usually ends in a ".pl".
Perl is the Practical Extraction and Report Language, written by Larry Wall. It is sometimes reffered to as the "Swiss Army Chainsaw" because of is's vast uses and built in tools. System Administrators have used Perl for a long time, so it is not just for the Web. Some features that are desired for the Web are its text processing capablities and built in searching and formatting.
Besides that, it is relativly easy to use. While knowing just a tiny portion of Perl's extensive features, you can accomplish many tedious tasks rather quickly. There are so many built in functions and operators for manipulating data. Sometimes Perl can do in one line what would take many, many lines in C or C++. This allows us to concentrate on getting the main task done, and we do not have to worry about as many nitpicky details. Using many pre-built building blocks, and reusing existing code we can whip up a quick n dirty Perl script for almost any task.
The Perl motto is "there's more than one way to do it!" The thing that matters is the overall effect of your program. Exactly how you chose to do it is up to you. With Perl you can break away from the strict rules and guidlines of programming in C, and can express your artistic imagination and creativity.
The perl script must be marked as an executable file. To do this, you use the chmod (change mode) command:
chmod 755 script-name.pl
When the web server sees that a client has requested an executable file, it proceeds to execute it. But because a perl script is a text file, it does not contain any machine code in it. That is why it is important to tell Unix that this file must be parsed and executed by Perl. This is done by putting this statement on the very first line of the perl script:
#!/usr/bin/perl
The Perl executable, that parses and executes perl scripts is usually kept in the directory /usr/bin/ , allong with a lot of other Unix programs. On some systems it may be in /usr/local/bin, so try that if you get an error.
What happens next is limited only to the programmer's imagination. Just about anything a C program can do, Perl can do (And in fewer lines of code!). The script is executed, and it's output is sent to standard out.
Well now that we know about Unix, HTML forms, CGI, and Perl, lets begin the tutorial!