OK, at this point, we’ve broken down the guestbook script line by line. Over the next two primers, we’re going to alter it. In this one, we’ll play with the PERL branching method. In the next primer, we post the results to another page. Neither event is overly hard, but let’s go through it slowly so I’m sure you’ll understand it.
First off, here’s the script in action. Try it a couple of times, each time choosing a different answer or no answer at all to the newsletter radio buttons.
We’ll start with the new guestbook HTML code. You’ll notice it’s exactly the same as the last primer except I stuck in a couple of radio buttons asking of the user wants to subscribe to our newsletter or not.
The New Guestbook Branch HTML Code
Here is the new script we’re going to be working on. Again, the code in dark blue is the code that is new to the script from the last primer. If you know any JavaScript, this will look somewhat familiar to you.
The Branching PERL Guestbook Script
There’s no need to change any modifications at this time. If you upload the files to replace what you have up there, then the modifications will just remain on the file. If you rename this pup and upload it, then yes, you need to set the modifications on the new files you uploaded, 775 for the CGI and 644 for the HTML page.
Got it? Seen it? Did you scroll all the way down the script? Are there two new blocks of code? Both are blue. Look again if you didn’t see them.
PERL Branching Code
if ($FORM{newsletter} eq "yes") {print MESSAGE "We will subscribe you to our newsletter right away!nn"; } elsif ($FORM{newsletter} eq "no") {print MESSAGE "You should rethink not signing up for the newsletter.nn";} else {print MESSAGE "Oh, I see, you're too good to answer the question.nn"; } |
I want to point out right now that the two new blips of code are NOT exactly the same. Notice in this one, we are going to write to the email letter we gave the filehandle “MESSAGE”. The thank-you page code does not require a file handle. That is oh-so-very important.
What Does It All Mean?
That’s a new one, huh? Notice in the two conditions, I have the code “eq” stuck in there like so:
if ($FORM{newsletter} eq "yes") |
I’ll bet you can guess what that means, equals, right? That’s it. Yes, you can use the traditional (=) sign of you’d like, but one of the things I like about PERL is that it offers text equivalents.
When I deal with math or numbers, I use the binary operator (+,-,>>, etc.). But when I deal with text like above, I use the text operator.
There are a few others you might like to know about:
Operator | What It Means |
eq | Equal to |
ne | Not Equal to |
gt | Greater than |
lt | Less than |
ge | Greater than or equal to |
le | Less than or equal to |
We’ll use these a lot in some of the upcoming PERL primers.
We’ll use these a lot in some of the upcoming PERL primers.
if ($FORM{newsletter} eq "yes") {print MESSAGE "We will subscribe you to our newsletter right away!nn"; } elsif ($FORM{newsletter} eq "no") {print MESSAGE "You should rethink not signing up for the newsletter.nn"; } else {print MESSAGE "Oh, I see, you're too good to answer the question.nn"; } |
It’s the same code as above except there’s no MESSAGE filehandle. This is straight text to be printed to the page. Each line is well-formed using the quotes and semicolon at the end.
Depending on what the user chooses, this will print the appropriate text to the page. No problem.
Primer Six Assignment
OK, here’s the deal. You want to add a form select menu with four choices. For the sake of academic argument, you will ask how the user would like something shipped. Can you show the appropriate HTML code and then the appropriate code you’d add to the PERL script?
Have the PERL Script post a thanks for each of the shipping choices.
Here’s a possible answer.
(This will open a new window):