Installing a Perl Script
print "Content-type: text/html\n\n";
print "<title>CGI Test</title>";
print "<body><h1>I just wrote a web page using Perl!</h1>";
Now, you will need to know where Perl is on your Web server. This is because you may need to change the top line in this script. If you are unsure where Perl is, ask your Web host or Telnet your server and type the which perl command at the prompt:
Usually, Perl is located in /usr/bin/perl or /usr/local/bin/perl directory. When you know where Perl is, use that path after the # sign and ! symbol above. So, if you had to change it to /usr/local/bin/perl, then the first line would read:
Once that is settled, you need to save the file. Use the "Save As.." option and be sure you save it as plain text. Also, to get the right file extension, you'll need to place quotes around the filename when you save it. Most Perl programs are saved with a .pl extension, but many Web hosts require you to save your CGI programs with the .cgi extension. Be sure to find out before you try to run the script. Since many hosts seem to be using the .cgi extension, we'll use that here. If you have to use the .pl extension, you can do the same things we will— just use the .pl extension in place of the .cgi extension.
Now, as mentioned briefly a moment ago, you'll need to place quotes around the filename when you save it so you get the extension you want to have. If you do not, your filename will end up looking like:
What do I mean by the quotes? Suppose you want the file to be named test.cgi. You would need to save it as "test.cgi" when you type the name in the box. Look at the screen capture below to see the proper form for doing this type of save:
Once you have your file saved, it will be time to upload it using your FTP program. So, fire up the FTP client of choice and drill down to your CGI directory (usually this directory is named cgi-bin). Now, be sure before you upload the file that you switch the transfer method from "binary" or "auto" to "ASCII" (In WS_FTP, these are shown as radio buttons below the two file lists. Choose the ASCII radio button).
Once you have that option set, go ahead and upload the file. You'll now have the file ready for one more thing: You will need to set the permissions for the file. If you have WS_FTP, right-click on the filename in the remote host area. Now choose: chmod (UNIX). You will get a dialogue box with nine check boxes in three columns. Under the "Owner" column, check all three boxes (read, write, and execute). In the "Group" and "Other" columns, choose only the top and bottom boxes (read and execute). Now it the "OK" button and you'll have it set. We are setting the permissions to chmod 755. This is a common setting for CGI programs. For those of you with other FTP clients, see if they offer a similar option. If so, you want to have chmod 755. If not, you'll need to do this through Telnet. First, change the directory so that you are in your cgi-bin directory. Ask your Web host if you are unsure of the path to get there. It will be a path similar to this:
It may be different on your server, so double check to be sure you know where to go. The command you want to use to change the directory is the "cd" command. So, you would type in cd followed by the directory path for your CGI directory, something like this:
Once you are in the directory, you can use the chmod command. The Telnet (Unix) command for this is simply chmod 755 filename. For example:
Telnet/cgi-bin>chmod 755 test.cgi
You can find out more about what the file permissions mean and how to get the correct numbers by looking at: The CHMOD Explanation.
Now, before you try running the program, you'll want to be sure you debug it through Telnet first. This is so you don't cause unnecessary errors on your Web server (Error code 500 is usually from a script that isn't working). To do this, you can run the debug option for your script. To do this, you would use the perl command with the -d switch. So, you would use perl -d filename. In our example, it would look like this:
Telnet/cgi-bin>perl -d test.cgi
This will show you the first line of the script. You can go through each step of the script by typing the letter s and then pressing the Enter key for each step. If you want to quit, type the letter q and press the Enter key.
If it passes the test (no errors), you can now try out the script. You can test it in Telnet by running it at the command line. Just take off the -d switch in the command we used to debug the script:
Of course, it prints out everything- even the HTML tags are printed as text and not as HTML. The HTML will work like you expect it to in the browser, which is what is next.
To run this in your browser, you can simply type in the URL for your CGI script- it should look similar to this:
The result should be a heading that says:
I just wrote a Web page using Perl!
You can also link to the script from another page, by using the CGI URL as the link URL:
<a href="http://www.yourname.com/cgi-bin/test.cgi"> Try out my page made from CGI</a>
Well, now that you can install the script, let's get into the basics of Perl and how to use it for Web applications. So, on we go to Perl Basics: Output.