![]() |
Web Conference 2004Writing Perl/CGI Scripts for ITS/ASET Web servicesMore On Perl |
| <- Back - Passing Data | | | Up | | | Talking HTTP - Next -> |
There is no way you can learn everything about Perl in a single session. This is just a quick intro into the basics you may use when writing CGI programs. The rest is up to you if you wish to pursue it further. See the references section for recommended reading.
The "she-bang" or "hash-bang" line, #!/usr/local/bin/perl - This is always the first line of a Perl script, which indicates it is a script (not a "binary" or compiled program) and the location of the perl program that "interprets" the script. In this case, the location of the perl program is /usr/local/bin/perl. The forward slashes are a Unix idiom.
The print <<END; line "prints," or rather, displays
all of the lines after until the line, "END". The word "END" can be
any word. Print can be used in other ways, such as print
"here is a line\n";. The "\n" means, "print the end of a
line" and start the next line.
use CGI qw(:param); invokes use of a module, or feature of the Perl language. The module we want to use here is called "CGI" and is used to handle CGI script functions. The qw(:param) part indicates we want to use the param() function. The param() function allows us to discover and use CGI parameters sent by the browser.
param('color') returns the parameter named "color".
Since we used this on the line $color = param('color');, it will
store the value of the color parameter into the $color variable.
$color is a variable, or a part of the program that can hold a value, and vary between various values during the life of the program. In the case of the last program, we want to hold the value of the parameter we called "color".
In Perl, this specific type of variable is called a scalar, coming from the mathematical sense that this can be a numerical value that can scale over various numbers. Perl scalar variables can hold more than numbers; they can also hold letters, words and whole sentences, references to other variables and more. You can also read in entire files into the scalar variables (and in some cases, you do) but you should only do so when you know the files aren't too big to fit in the computer's memory. Perl scalar variables always begin with the dollar sign ($) to indicate what type of variable it is.
Perl has other variable types, such as arrays or lists of values stored together, and hashes or associative lists that link names to values. Arrays are listed with an @ sign in front of them, such as @simple_array; hashes are listed with a % sign in front of them, such as %simple_hash. When referencing a specific element of an arry or hash, you treat it as a scalar and thus use the $ again, such as in $simple_array[0] or $simple_hash{'tree'}. The brackets [] indicate it is an element of an array, and the braces {} indicate it is an element of a hash. You reference arrays by number and hashes by another scalar.
In programming languages such as Perl, it is possible to break up the program into parts or blocks which you can call from other parts of the program or run multiple times. Control blocks organize these blocks and assign code to determine when to run the code in the block.
Control statements such as if, else, while, for and foreach are just a sample. Here is an example of an if statement:
#!/usr/local/bin/perl
use CGI qw(:param);
$color = param('color');
print <<END;
Content-Type: text/html
<html>
<body>
<p>You favorite color is $color.</p>
END
if($color eq "blue"){
print "<p>Right. Off you go.</p>\n";
}else{
print "<p><font color=red><i>INTO THE GORGE YOU GO!!</i></font></p>\n";
}
print <<END;
</body>
</html>
END
|
In this case, it will seem to be the same script as passingdata.cgi, however it will give a little twist to your choice. If you choose "blue" as your color, it will grant you safe across the Bridge of Death. Otherwise you are thrown into the Gorge of Eternal Peril (*).
* - Reference to Monty Python's Search for the Holy Grail |
| <- Back - Passing Data | | | Up | | | Talking HTTP - Next -> |
If you have any questions, feel free to ask me - mailto:jcd@psu.edu
Content by: Jeff D'Angelo <jcd@psu.edu> © 2004
Last update on: Sunday, 13-Jun-2004 17:28:08 EDT