1 - Background Information
If you run a website, you've probably heard of either Perl or CGI at some point. Whether you've merely heard of it, installed a few scripts on your own, or taken one look at the code and turned pale, this tutorial is for you.
Perl is a programming language. Many people think CGI is a language, as in, "I'm going to learn CGI," but this is erroneous. Think of CGI as the method in which you use Perl. You can write CGI scripts using other programming languages, but Perl is far the most common due to its relative simplicity and power.
Even as more efficient programming languages are starting to take away Perl's popularity, Perl programming remains very popular and learning it is not a waste of time. For example, PHP is similar to Perl in most respects. The syntax used is similar and a lot of the basics are somewhat the same. If you know Perl, you've got a huge head start at learning PHP, and a few other languages as well.
Although Perl may be less efficient than other programming languages like PHP or ASP, there are still infinitely more freeware and shareware scripts out there written in Perl than in any other language. In other words, Perl isn't going away anytime soon.
2 - A Few Small Rules
Now that you have a little background on Perl, let's get down to some of the basic commands.
Like any programming language, Perl has rules. You will need to use certain symbols in certain places to tell the script something. One of the most basic rules in Perl is that every line must end with a semi-colon: ";" . As with every rule there are a few exceptions which we'll go into later, but for now just realize that almost every line of Perl code that you write will end with a semi-colon. The semi-colon, in case you're wondering, tells Perl that it has reached the end of a command.
Let's write a quick script to introduce you to some of the basics.
In any Perl-based script, a line like this goes at the very top:
This has been nicknamed the "shebang" line, and it is another requirement of the Perl language. The shebang line points the browser to the path to the Perl interpreter on your web host's server. Think of this line as a way of telling your browser, "Here's where you can find a translator to turn my code into a functional script."
Virtually every web host has Perl installed, although it will not always be in the location specified above. If the above line doesn't work, contact your system administrator and ask them for the path to Perl.
Note that you'll need to point to the Perl interpreter in each and every file that you write. (Note: files that end in .cgi, .pl, and .perl can all be considered Perl files.) This tutorial from now on assumes that you have a basic knowledge of HTML and form fields. If you don't, head on over to HTMLGoodies.com and read through some of the tutorials offered there.
Let's create our first script!