[Urgent] PERL assistance - Basic arithmetic operations code.

Discussion in 'Programming' started by nish7x, Aug 2, 2011.

  1. #1
    I got a college assignment to make a page in PERL. We're not taught anything so I solely relied on the web. Have to make a page with text input fields for 2 numbers, a drop-down menu for arithmetic operation and a Result text field. When you click on a Calculate or Submit button, it returns back the result in that text field. I've managed to do the addition part, however the rest of the operations don't work (Sub, Mul, Div). Probably because I'm not applying the LOOP thing properly, or not implementing the dropdown thing well. I'm a complete newbie so no idea how to go ahead. Please help if possible. Thanks.

    Demo of page: http://nishant.me/cgi-bin/perl1.cgi

    Code:

    #!/usr/bin/perl
    use CGI::Carp qw( fatalsToBrowser );
    
        local ($buffer, @pairs, $pair, $name, $value, %FORM);
        # Read in text
        $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
        if ($ENV{'REQUEST_METHOD'} eq "POST")
        {
            read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        }else {
        $buffer = $ENV{'QUERY_STRING'};
        }
        # Split information into name/value pairs
        @pairs = split(/&/, $buffer);
        foreach $pair (@pairs)
        {
        ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%(..)/pack("C", hex($1))/eg;
        $FORM{$name} = $value;
        }
    
        if ($ENV{'REQUEST_METHOD'} eq "POST")
        {
            read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        }else {
        $buffer = $ENV{'QUERY_STRING'};
        }
        # Split information into name/value pairs
        @pairs = split(/&/, $buffer);
        foreach $pair (@pairs)
        {
        ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%(..)/pack("C", hex($1))/eg;
        $FORM{$name} = $value;
        }
       
        $number1 = $FORM{number1};
        $number2 = $FORM{number2};
        $operation = $FORM{operation};
    #    my @operation = @{$self->{operation}}
    
        if ($operation = "add") {
                 $result = $number1+$number2;
        } elsif ($operation = "sub") {
                 $result = $number1-$number2;
        } elsif ($operation = "mul") {
                 $result = $number1*$number2;
        } else {
                 $result = $number1/$number2; }
    
    
    print "Content-type:text/html\r\n\r\n";
    print "<html>";
    print "<head>";
    print "<title>Perl Program for Arithmetic Operations</title>";
    print "</head>";
    print "<body>";
    
    print "<FORM action='perl1.cgi' method='POST'>";
    print "Number 1: <input type='text' name='number1' value='$number1'>  <br>";
    
    print "Operation: <select name='operation' id='operation'><option value='add'>add</option><option value='sub'>sub</option><option value='mul'>mul</option><option value='div'>div</option></select><br>";
    
    print "Number 2: <input type='text' name='number2' value='$number2'>  <br>";
    
    print "Result: <input type='text' name='result' value='$result'>  <br>";
    
    print "<input type='submit' value='Calculate'>";
    print "</FORM>";
    
    print "</body>";
    print "</html>";
    
    1;
    Code (markup):
    Thanks a lot! :)
     
    Last edited: Aug 2, 2011
    nish7x, Aug 2, 2011 IP
  2. i8k

    i8k Greenhorn

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Check to make sure that you're variables are being passed by adding them into the title :

    print "<title>[ n1: $number1   n2: $number2  op: $operation   result: $result     ] - Perl Program for Arithmetic Operations</title>";
    Code (markup):
    Also keep in mind that the root of what you are doing is very simple :

    
    my $result = $thisnumber - $thatnumber;
    print $result;
    
    
    Code (markup):
     
    i8k, Aug 2, 2011 IP
  3. nish7x

    nish7x Well-Known Member

    Messages:
    120
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    130
    #3
    True, but I can't quite understand how to get the value of the SELECT dropdown menu. That is, if it is ADD then the result should be $number1 + $number2. If it is SUB then it should be $number1 - $number2, and so on.
     
    nish7x, Aug 3, 2011 IP
  4. nish7x

    nish7x Well-Known Member

    Messages:
    120
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    130
    #4
    I found the solution. Changed the IF, ELSIF to 4 different only IF loops, without any ELSIF. So the new loop was:

        if ($operation eq "add") { $result = $number1 + $number2; }
        if ($operation eq "sub") { $result = $number1 - $number2; }
        if ($operation eq "mul") { $result = $number1 * $number2; }
        if ($operation eq "div")
        {
            if ($number2 == 0)
            {
                $result = "Cannot divide by zero.";
            }
            else { $result = $number1 / $number2; }
        }
    Code (markup):
     
    nish7x, Aug 3, 2011 IP
  5. i8k

    i8k Greenhorn

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    Cool, good job, my only suggestion depending if you want to impress the teacher or not :) is to use CGI form code as opposed to the old style stuff, it would cut a lot of overkill out and make it much cleaner. But on the other hand it it ain't broke don't fix it right.
     
    i8k, Aug 3, 2011 IP