Article
Programming Perl 101
Now, copy and paste this into the display.cgi file:
print "Content-type: text/html\n\n";
print "Thank you, here is the information you entered:<br>";
print "<ul><li>$first</li><li>$city</li><li>$food</li></ul>";
That first line tells the script that you'll be using HTML in one or more of the "print" statements. Now that the information from the form has been assigned new variables beginning with the $ sign, all we have to do is type those variable names out and the script will display what the user entered.
The print statement that you see above simply tells Perl to spit out the HTML code out to the browser after parsing it. Here's what Perl "thinks" when reading the above three lines:
Line One: Hmm... we'll be working with HTML statements in one or more of the following print statements.
Line Two: Ok, this one is simple. I'll just spit out the code between the quotation marks back out to the browser.
Line Three: This line contains three variables named $first, $city and $food that I need to find and replace before sending out this line of code to the browser. I'll take the value of $first from the form field named "first" in the form that was just submitted to me, then I'll do the same for $city and $food. Now that I know what $first, $city and $food stand for, I can send this line of code to the browser with the replacements made.
Here's the entire display.cgi file:
#!/usr/local/bin/perl
require "subparseform.lib";
&Parse_Form;
$name = $formdata{'name'};
$city = $formdata{'city'};
$food = $formdata{'food'};
print "Content-type: text/html\n\n";
print "Thank you, here is the information you entered:<br>";
print " - First Name: $first<br> - City: $city<br>";
print " - Favorite Food: $food";
Let's assume the user entered the name "John," the city "New York," and the favorite food as "Pizza." Here's what John would see displayed in front of him after filling out the form:
Thank you, here is the information you entered:
- First Name: John
- City: New York
- Favorite Food: Pizza
You should now have a basic grasp of some of the rules of Perl and how slapping identifying "tags" on data can be useful. As you'll also learn later on, you can tell Perl to send out emails using these same variables (tags). Imagine having someone enter his email address and name, and getting an email personalized with his name in the subject line!
In Part 2 we'll try to drill some syntax laws into your head, explore "if," "elsif," and "else" commands, and even explore using sendmail to send out automatic confirmation/thank you emails!