Quick Start Guide for Beginners to Perl / CGI
What is Perl?
Perl is a program that works behind HTML, and enables your web site to work with a more dynamic feel.
For an example let’s use the Novice Forms script. You would setup an HTML form page setting all the variables, this is all done in HTML, no Perl would be on this page. Then the form contains a html "POST" statement, that posts to the Novice Forms Perl script. Then the Perl script process’s the information and sends you back to a success HTML page.
Here is a quick tutorial for individuals that are new to CGI and Perl.
Note: ALL example Perl Code is colored red.
This tutorial is written in English that you can understand, and less technical Jargon you can't.
Firstly all Perl scripts need to start with the path to Perl which is usually:
#!/usr/bin/perl
The path to Perl always needs to be alone on the very first line of each Perl Script with no spaces or characters before the pound sign. Basically this is where your script will find Perl on your server. Check with your host or systems administrator if you are unsure as to what the path is.
Code in perl is commented with a #
Commenting is used to make notes in scripts, so if you want to go back and edit a script you created a year ago, you will know what you where trying to accomplish. All commented parts of your script will not show up on your page, or run through the script. When you stop commenting you must hit the enter key and start a new line to start re-coding Perl.
Example:
# THE BELOW VARIABLE IS SET TO VALUE "will run" - #
#(THIS LINE AND THE ABOVE TWO LINES ARE COMMENTED #
#AND WILL NOT SHOW UP OR RUN) #
$variable = "will run";
The escape character in Perl is a \
This is used to escape characters that declare variables and arrays.
In actual Perl code if you need to use the \ as a regular character and not as code use: \\
The Escape character \ will be explained more in depth below.
Common problems
Firstly, @ signs in Perl represent an Array, which is a list of data separated by a coma.
Here is the problem, since the @ is code, when you use it as a regular character, LIKE FOR INSTANCE in an email address, you must use the escape character \ as explained above. So your email address must look like this: you\@yourdomain.com
This way Perl does not think your email address that you are typing in your script is an array.
Lastly on creating a Perl script, all scripts need a .cgi or .pl extension at the end of the file name.
Example: script.cgi
One other problem you may run into if you are running on a WINDOWS server. If your Windows server does not run fast cgi
you need your Perl scripts to have an ending extension of .pl not .cgi.
So for the Novice Forms script you would need to rename it from (novice.cgi TO novice.pl).
Example:
GOING FURTHER... Definitions
Variables - In English, a variable is comparable to an algebra equation like: X = 3 + 2, Where X would be a variable. In perl all variables start with the $. An example of setting a variable would be:
$ccto = "you\@yourdomain.com";
So anywhere in your script that contains $ccto would print you\@yourdomain.com which would convert into you@yourdomain.com when page is viewed, because as stated above, the @ needs to be escaped. ALSO since the $ starts a variable, anything with a dollar sign will be treated as a variable. So if you want to use a $ as a regular character you must use the escape character before it. Example: \$
Breaking the Array Statement Down...
1. Declare that you are writing a variable by starting your statement on its own line with the $
2. Name the Variable. (Variables can be named with: ANY NUMBERS, LETTERS, and UNDERSCORES.) Example: $somevariable
3. Set the array = to a value which the value should be surrounded by " and a ; should follow the second " .
4. Complete Variable Example: $somevariable = "value";
Other Possible Problems...
Notice the variable is surrounded by two ". So what’s the problem? The problem is this.
If you want to put a " in the value part of the statement, Perl will think you are trying to end the statement and you will get a 500 Internal Server Error.
The Solution...
Remember the \ escape character. Guess what when you want to use a " as part of a variable value you have to escape it \".
Technical Fact: the above variable in Perl is called a Scalar Variable.
Array - In English, as stated above an Array is a list of values that can be used latter in the script. We are only going to go over how to write an array, not how to retrieve the variables from it, since you will need to know how to add values to an array in this script.
Sample Arrays:
@somearray = ("value1", "value2");
-or-
@something = ("value1", "value2", "value3");
-or-
@vals = ("value1", "value2", "value3", "value4");
-or-
@okurls = ("http://www.yourdomain.com/", "http://yourdomain.com/", "http://234.322.345.343");
Breaking the Array Statement Down...
1. Declare that you are writing an array by starting your statement on its own line with the @
2. Name the array. (Arrays can be named with: ANY NUMBERS, LETTERS, and UNDERSCORES.) Example: @somearray
3. Set the array = to a list of values.
4. List array values. The values should start with a ( Followed by all values surround each with a " and separated each with a coma then space then REPEAT for each value. Note that on the final variable there should be no characters or spaces after the final " just the ending ); Example: ("value1", "value2");
5. Complete Array Example: @somearray = ("value1", "value2");
Also note that the end of a declaration statement for any array or variable is the ;
Other Stuff you need to know after you have a complete CGI / Perl Script
Great we have a complete CGI / Perl script, that is fully configured the way I want it... NOW WHAT? Good Question. The first thing you must do is FTP the script onto your server or web space. You must FTP Perl Scripts to your server, online upload pages will not work in almost all cases. The most commonly used FTP client is CUTE FTP. Ok so I have my FTP client and I am logged into FTP with the site I want to run my Perl script, now what? Well Perl scripts as stated in the beginning of this resource are different than HTML pages, although they contain HTML. Now you must upload and CHMOD your script.
How To upload and CHMOD.
1. You must upload your Perl Scripts to your cgi-bin.
(With few exceptions but this would depend on your servers configuration).
2. Once uploaded you must set file permissions or CHMOD all scripts to 755, 766, 777. However 755 is the most common to set to AND IS WHAT I RECOMEND YOU TO USE UNLESS YOU HAVE A SPECIFIC REASON. ALSO 777 is generally not a good choice since this creates MAJOR security flaws and gives visitors the ability to write to your Perl script. So if they can find other flaws in your server they could possibly hack your script and/or server. Some people use other permissions but these are the most popular.
What does CHMOD mean?? CHMOD stands for Change Mode which is also referred to as file permissions.
To CHMOD files in Cute FTP simply right click on the file you want to CHMOD,
Then left click on CHMOD.. This will pop a "Change File Attributes" box.
In the text box to the right of "Manual" Erase the existing number and replace
it with number you want to set permissions to. Then click "OK". That's it, follow the same
process for each file you want CHMOD.
Setting file permissions on Windows servers can be different, so you may need to contact your host or System Administrator for help.
............................................................................................
File Permissions or CHMOD REFERENCE:
CHMOD 777=
Owner: read, write, execute.
Group: read, write, execute.
World: read, write, execute.
CHMOD 766=
Owner: read, write, execute.
Group: read, write, ---
World: read, write, ---
CHMOD 755=
Owner: read, write, execute.
Group: read, --- , execute.
World: read, --- , execute.
............................................................................................
Need More Form Processing Features:
Biz Mail Form - is a Perl forms processor that sends data to a file and your email.
Think Your Smart? Take The Toughest & Funniest IQ Test!
Need A Good Laugh? Visit the funniest music site ever; No Refunds Music!
© Copyright 2003-2004 Novice Forms Processing, All Rights Reserved by Seth M. Knorr.
|
|