1. All Perl scripts must begin with the following statement, on the first line:
Because Unix does not map file suffixes to programs, there has to be a way to tell Unix that this file is a Perl script, and that it is to be executed by Perl. This is seen before in shell scripts, in which the first line tells Unix to execute it with one of the shell programs. The Perl executable, which will take this file, parse it, and execute it, is located in the directory /usr/bin. This may be different on some systems. If you are not sure where the Perl executable is, type which perl on the command line, and it will return you the path.
2. All scripts should be marked as executable by the system.
Executable files are types that contain instructions for the machine or an interpreter, such as Perl, to execute. To mark a file as executable, you need to alter the file permissions on the script file. There are three basic permissions: read, write, and execute. There are also three levels of access: owner, group, and anyone. Perl files should have their permissions changed so that you, the owner, has permission to read, write and execute your file, while others only have permission to read and execute your file. This is done with the following command:
chmod 755 filename.pl
The number 755 is the file access mask. The first digit is your permission; it is 7 for full access. The user and anyone settings are 5 for read and execute. Normal files (html files) have the access mask of 644, which allow everyone to read, but only the owner to write to the file.
If you only have FTP access to your web site (You aren't given a login shell to type commands), you can still change the file access mask by sending the right command through your FTP program. Most of them allow you to send an arbitrary command. Find this and type:
site chmod 755 filename.pl
You should only have to change the file permissions once, when you first upload the perl script. Every time you re-upload and overwrite the existing file, its file permissions stay the same.
3. All Perl scripts should be uploaded in TEXT mode.
Most FTP programs have two options for uploading, TEXT and BINARY. You use binary for uploading GIFs, JPEGs, and anything that is not strictly text. Use TEXT mode for uploading HTML fies and Perl files. (HTML can be uploaded in either mode, but it is best to use TEXT mode). Unix files are usually straight ASCII text, but some files (executables, image files, etc.) are stored as binary. The reason why you must use text mode is that Unix interprets the end of line character differently from DOS and Macintosh systems. When you use text mode in your FTP program, it takes care of translating the end of line characters for you. If you don't do this, your Perl script will not execute and you will get an error.
4. The very first print statement in a Perl script that returns HTML should be:
print "Content-type: text/html\n\n";
When your Perl script is going to return an HTML file, you must have this as the very first print statement in order to tell the web server that this is an HTML file. There must be two end of line characters (a \n) in order for this to work. After that, you usually print "<HTML><BODY>"; etc.
5. Always use the POST method with your HTML forms
There are 2 ways to get information from the client to the web server. The GET method takes all of the data from the forms and concatenates it onto the end of the URL. This information is then passed to the CGI program as a command line argument. Because the GET method has the limitation of being 1024 characters long, it is best to use the POST method. This takes the data and sends it allong with the request to the web server, without the user seeing the ugly strings in the URL. This information is passed to the CGI program through standard in, which the program can easilly read from. To use the POST method, make sure that your HTML form tag has
METHOD=POST (no quotes).
6. HTML forms must reference the perl script to be executed
In your FORM tag, there is an ACTION attribute. This is like the HREF attribute for a link. It should be the URL of the CGI program you want the form data sent to. Usually this is
7. Perl files usually go in the cgi-bin directory of your web server.
The web server has a "root" directory. This is the highest directory your HTML files can access. (You don't want clients to be able to snoop around your entire system, so the rest of the system is sealed off above this "ceiling") in this directory, there is usually one called cgi-bin, where all the CGI programs go. Some web service providers give each user a cgi-local directory in their home directory where they can put their cgi scripts. If this is the case, use this one instead. You usually have to have permission to put files into the cgi-bin directory, so ask your site administrator about their policy on cgi-bin access.
8. Perl files end in .pl :-)
Just a convention, to make things easier on programmers.
9. All statements end in a semicolon
Don't forget this, because lots of beginners' errors come from not remembering the semicolon!