This article is part of our series of 10 Minute Quick Starts. Each quick start is step by step, assumes you know very little about the subject, and takes about 10 minutes. You can use them to scratch the service of areas you want to learn and as a quick review when returning to something after a long absence.
Perl
Common Gateway Interface (CGI) lets you run CGI scripts written in various scripting languages such as Perl and PHP. With CGI, you can add interactive and dynamic content to your Web page. If you are setting up your own webserver, you have to install Perl. If you're website is hosted with a hosting service, ask them if they support Perl and read up on any special rules they may have imlemented. Either way, you'll probably also want to setup a webserver and install Perl on your development computer too so you can easily and quickly develop code.
Install Perl
The first thing you must do is to install Perl or use a hosting service with Perl installed (most do offer Perl). If your web site is hosted with Prestwood, then your web site does have Perl installed. If you're hosting your web site with someone else, check with them to see if it is installed.
Installing Perl
[Optional] If you are developing on your development box, let's start by making sure Perl is installed.
Webserver. First you need a web server supported by a Perl installation.
Test if installed or not. If you're maintaining your own server, go to the server and execute the following command from a command prompt:
perl -v
If Perl is installed, the Perl version information will be displayed.
Install Perl if needed. There are many flavors of Perl available today. It is available for Windows, Mac, Unix, Amiga, and Linux to name a few. Click here to download the latest version of ActivePerl for Windows, Linux, or Solaris.
And here, we, go...
Part 1: First DOS Box Scripting Test
[Optional] If you are developing on your development box, let's start by printing Hello World either to a command prompt (DOS or Unix).
Create test.pl script file. If you have Perl installed locally (or you're working on the server), type and save the following Perl script as test.pl:
print("Hello");
Note: Perl is case sensitive so make sure you use all lowercase for the print command. Also, do not forget the statement ending semi-colon.
Run simple test.pl script. At the command prompt, type the following to execute the program:
perl test.pl
If all went well, you should have seen "Hello" printed to the screen. This tells you that you have Perl installed correctly.
Try a syntax variation. With the print command you can also leave off the () and just use a space to separate the print command and the literal string. Add the following line of code to your short test.pl script:
print "I am a Perl coder!";
Run your Perl program again.
Part 2: Browser Example
This next example creates a complete HTML page from Perl.
Create helloworld.pl script file. Type the following Perl script into an editor and save it as text in a file named helloworld.pl.
#Start of HTML page. print(""); print("Perl Tutorial"); print(""); print("Hello World"); print("");
Upload to server. Next copy or FTP your short Perl script to your server. Many servers are configured to allow Perl scripts to execute only from a /cgi-bin/ folder. Check with your hosting provider and ask which folder they have configured to run Perl scripts.
Execute script. Open a browser and browse to the URL of the script. Something like:
Note: Some hosting services use cgi-local for customer scripts and cgi-bin for hoster-provided scripts.
Or, if you are developing locally on your development computer, you will browse to something like:
/cgi-bin/helloworld.pl
Here is sample output in the Google Chrome browser:
Code Explanation
#!/usr/local/bin/perl -w
The first line of this script is a special comment that indicates the location of the Perl program. Depending on your setup, you may be able to leave this line off. Unix users must include this line with the correct full path to the Perl program.
The -w Command Line Option
The command line optional option -w on line 1 tells Perl to syntax check your code for errors and if any are found, display them. This command line option is often removed just prior to deploying your script.
print("Content-type: text/html\n\n");
This line tells Perl to write out a standard HTML text-based page. It sets the mime type.
If you view the source code for the returned browser page, you'll see it's HTML code:
#Start of HTML page.
This is a regular comment. Perl uses a # for comments and you should get in the habit of commenting your code liberally.
These print commands write out a standard typical HTML page. Notice Perl uses semi-colon to mark the end of statements. Leave off a semi-colon and refresh your browser to see what type of error is generated.