Please note, this is a STATIC archive of website www.htmlgoodies.com from 16 Nov 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
Wednesday, November 16, 2022

HTML Goodies: PERL Primer, Part 9

…use these to jump around or read it all


[The HTML and PERL]
[How It All Works]
[Assignment]

     Performing math within PERL is pretty easy. There are just a few rules to follow to get the correct display, but other than that I’ve never had any trouble. I don’t think you will either.

     The script in this lesson shows a couple of math equations that demonstrate how you’d put together just about any other equation. Get these and you’ll be able to build just about any other.



The HTML and PERL

     Here you go. Give this a shot.

Do Some Math

     The interface is fairly straightforward HTML. It looks like this:


The Math HTML Code

     The text box that accepts the single digit is named “singledgt”, the box that accepts the double digit number named “doubledgt” and finally the box that accepts the amount you’d pay for dinner is named “dinner”. Now the PERL Script. I’ll open it in a new window so you can refer to it during the discussion.


Here’s the script



How It All Works

     You should recognize the first module of code. It’s the exact same module we’ve been using to gather the information the user put into the form. The module then uses the command “&thank_you;” to trigger the thank-you page to display. Of course it’s not even really a thank you page here, but what the hey. Why change more code than you have to?

     Alright, let’s get into some math! First off we’ll simply display the numbers the user put in. There’s nothing new here. Notice that the line is well-formed with the three scalar variables representing the three inputted items from the form. The line has the text to be printed in double quotes and it ends with a semicolon.


Add Them Together

     You’ve probably seen this before but the basic math operators are:



  • (+) Addition
  • (-) Subtraction
  • (*) Multiplication
  • (/) Division

     You should learn this rule too: Text to be printed “AS IS” is placed within double quotes. Variables that are to be manipulated in some way are not in quotes.

     That’s why if I use $FORM{singledgt} within quotes, the number it represents just displays. If I don’t, the number can be played with.

     But, no matter what you’re doing, quotes or no quotes, the line must end with a semicolon.

     Let’s add it. I set it up so there’s some beginning text that reads, “Did you know that # plus ## equals:”. Then I get the answer by adding the two scalar variables together:


print $FORM{singledgt} + $FORM{doubledgt};

     I want that amount to display, so I make it a print command.

     There’s no reason why I needed to print the results like I did. I could very well have said:

$added = $FORM{singledgt} + $FORM{doubledgt};

     Then I could have printed that new scalar variable or held it for use later on in the script. The way I did it, I felt, was just easier.


Square It

     Once again, some introductory text and then we take the single digit and times it by itself:

print $FORM{singledgt}*$FORM{singledgt};

How Much Bigger?

     This is done the same way except I have some introductory and post text surround the smaller number being taken away from the bigger number:

print $FORM{doubledgt} – $FORM{singledgt};

The Tip

     Allow me to point out a concern that I learned through getting error after error until a light bulb went off in my head. I am working with a dollar figure here, right?

     Well, the last thing you want is a user putting in a dollar sign and you certainly cannot ask a print line to print a dollar sign. Why? Because that dollar sign means a scalar variable. Error!

     There are other methods of getting around the dollar sign problem, but my favorite is the ASCII command. When you need a dollar sign in the text, use this: $. It solved all my problems. Notice also that I put a dollar sign outside of the text box where the user is to put in a dollar amount. I’m hoping that stops him or her from putting in his or her own dollar sign.

     Tips are figured by multiplying the amount by 15% or .15. If you want the total, take the amount paid plus the tip. Remember algebra class? Put smaller equations in a larger equation inside (parentheses). It looks like this. Notice the use of the ASCII dollar sign:

print $FORM{dinner}*.15;

print ” for total of $”;

print $FORM{dinner}+($FORM{dinner}*.15);

     Easy enough…


Check That Dinner Amount

     I want to know if a person is willing to spend $100 of more on dinner, so I set up a little branch that tests if the dinner amount is less than 100:

if ($FORM{dinner} < 100)

     I print one line if it is and another if it isn’t.

     We wrap up the script by finishing out the well-formed HTML page. That’s about all. You should be able to put together just about any math equation with just what you got here.



Primer Nine Assignment

     Up above I used addition, subtraction and multiplication. Let’s get you to use division.

     Set up a text box that asks the user to put in a number of feet. Then, using PERL, convert that number of feet into centimeters.

     By the way, there are around 2.2 centimeters per inch. I said per inch, not foot. Can you do it?

Here’s a Possible Answer
This will open in a new window

On to Primer Ten



[The HTML and PERL]
[How It All Works]
[Assignment]

Popular Articles

Featured