Programming Perl 101

2 - Test.cgi
Create a text file and save it as "test.cgi"…as usual, you'll need the subparseform.lib file uploaded to the same directory as test.cgi. As before, if you cannot find this through a web search, email me and I'll send it to you as an attachment.

On with things; enter this into test.cgi:

#!/usr/local/bin/perl
require "subparseform.lib";
&Parse_Form;
$action = $formdata{'action'};

As you can see, we have the standard shebang line first. Secondly, we have a command that makes sure that subparseform.lib is in the correct directory. Third, we have a subroutine (don't worry about that word for now) that is being called to help us process the data being send from test.html, and fourth, we have a line that takes the value of the action field from test.html, and assigns it the variable "$action."

Next, enter this:

print "Content-type: text/html

";
 

That tells the script that we might be using some HTML in our print commands…even if you don't use any HTML, I recommend using this line in your scripts at all times to avoid potential problems.

Now, enter this:

if ($action eq "1") {
print "You must have clicked the first button!";
}

It's not quite plain English, but it's a lot easier to understand than a lot of the other things in this tutorial. The above is called an "if" command. See the first line there? Translated into English, it would read "If the variable '$action' is equal to 1." Notice the opening bracket right after this statement, and the closing bracket after the print statement. Do you see where this is going?

The "if" statement is basically telling the script that if $action is equal to "1", then process the code inside those two brackets. If someone clicked, say, the second button, then the $action variable would equal something other than 1, and the code inside those brackets would not be processed - the script would simply skip over it.