Thursday, March 28, 2024

HTML Goodies: PERL Primer, Part 6

…use these to jump around or read it all


[PERL Branching Code]
[What Does It All Mean?]
[EQ?]
[Thank You Page Text]
[Assignment]

     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.

Branching Guestbook Example

     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

     All programming languages have some kind of branching methods to allow programs to act one way or another depending on what the user will do.

     The basic concept is that if the user does this, then the program will do this. If the user does something else, the program will do something else. It can get super complicated if you ever get into serious programming. Take a car race game for instance. Think about all the choices a user has to pick from at almost any time. Your programming has to take all of those choices into account and offer a response for each. It can become mind boggling I’m sure.

     Luckily, here we’re only dealing with the user making one of three choices. Yes, I know
there are only two radio buttons, but if you think about it, the user really has three choices. He or she could either choose, yes, no, or not choose at all. That’s three and we have to be prepared to react to each one.

     In this case, we’re going to write something to the email and to the thank-you page, depending on the choice the user makes.

     If they choose yes, we’ll post the text, “We will subscribe you to our newsletter right away!”. If they choose no, we’ll ridicule them a bit with, “What?!?!? Our newsletter isn’t good enough for you? Huh?!?!?.” Finally if they don’t make a choice, we’ll simply insult them with, “Oh, I see. You’re too good to answer the question!”.

     That should be fun, yes?

     The new HTML form elements were given the NAME “newsletter”. That seemed appropriate. As you learned in the last couple of primers, we can now use the choice made by the user through $FORM{newsletter}.

     One more thing, these were radio buttons so we needed to give them values also. There were given the values “yes” and “no”. Notice the values are in quotes denoting they are text strings. That’s how we’ll test for them also, with quotes.

     Here’s the code from the email letter:


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?

     The way it was explained to me was that if you have only two elements to choose from, you could use a basic if/else format. If this, then do this, else, do this. The problem is that the choice must be yes or no, black or white. Those don’t pop up too often in programming.

     If you have three or more, you use an if/elsif/else format. Let’s say you offer five choices to the user. The list of branching would go like this:


  • if (something)
  • elsif (something)
  • elsif (something)
  • elsif (something)
  • else

     There’s no (something) after the final “else” because if doesn’t test anything. It simply
acts as a catch-all. If the top items are all not applicable, do the “else”.

     Please notice! The command is “elsif” not “elseif”. There is no second “e”. That messed me up pretty good not too long ago.

     The format is quite rigid and you have to follow it to the letter. The “if” and “elsif” conditions must be within (parentheses). After each condition, you’ll have the even that should occur if the condition is met. That event must be in curly brackets.

     Please notice the event that is within the curly brackets is well formed. The print
statement has the text in double quotes and finishes up with the required semicolon.

     Again, there is no condition after the “else” condition because that one only happens if the above conditions are not met.



EQ?

     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
eqEqual to
neNot Equal to
gtGreater than
ltLess than
geGreater than or equal to
leLess than or equal to

     We’ll use these a lot in some of the upcoming PERL primers.



Thank You Page Text

     It looks like this:

if ($FORM{newsletter} eq “yes”)

{print “We will subscribe you to our newsletter right away!nn”; }

elsif ($FORM{newsletter} eq “no”)

{print “What?!?!? Our newsletter isn’t good enough for you? Huh?!?!?.nn”; }

else

{print “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)

On to Primer Seven…


[PERL Branching Code]
[What Does It All Mean?]
[EQ?]
[Thank You Page Text]
[Assignment]

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured