Error handling won't go away!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JWest46088
    New Member
    • Sep 2006
    • 74

    Error handling won't go away!

    I'm having problems with my error handling. It's kind of hard to explain so I'll just post my code and bold where the error handling is and then explain what is happening.

    Just to warn you, I am new to Perl so the code probably will be ugly!

    Code:
    #! /usr/local/bin/perl
    
    BEGIN
    {
    	open(STDERR, ">&STDOUT");
    	select(STDERR); $| = 1;
    	select(STDOUT); $| = 1;
    	print "Content-type: text/html\n\n";
    }
    
    if($ENV{'QUERY_STRING'} eq "")
    {
    	&page1;
    	exit;
    }
    
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    
    if($buffer eq "")
    {
    	$buffer = $ENV{'QUERY_STRING'};
    }
    
    @pairs = split(/&/, $buffer);
    foreach $pair(@pairs)
    {
        	($name, $value) = split(/=/, $pair);
    
    	$value =~ tr/+/ /;
        	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        	$name =~ tr/+/ /;
        	$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    
    	if($FORM{$name} eq "")
    	{
    		$FORM{$name} = $value;
    	}
    	else
    	{
    		$FORM{$name} .= " " . $value;
    	}
    }
    
    print <<ENDHTML;
    <HTML>
    <HEAD>
    <TITLE>Assignment #4</TITLE>
    </HEAD>
    <BODY text="white" bgcolor="blue">
    <FONT face="arial" size="4">
    <DIV align="center">
    <br>
    ENDHTML
    
    sub page1
    {
    print <<ENDHTML;
    <HTML>
    <HEAD>
    <TITLE>Assignment #4</TITLE>
    </HEAD>
    <BODY text="white" bgcolor="blue">
    <FONT face="arial" size="4">
    <DIV align="center">
    <br>
    
    <FORM METHOD=GET ACTION="http://cs.sunyit.edu/~westj2/cgi-bin/assignment4.pl/page2">
    
    Enter number of elements to add and average:&nbsp
    <input type="text" name="numelements" value="" maxlength="7"><br><br>
    
    <input type="submit" name="submit" value="Submit">&nbsp&nbsp
    <input type="reset">
    </FORM>
    </DIV>
    </FONT>
    </BODY>
    </HTML>
    
    ENDHTML
    }		
    
    [B]if($FORM{'numelements'} eq "")
    {
    	print "Invalid number of elements!";	
    }
    else
    {
    	&page2;
    }[/B]
    
    sub page2
    {
    print <<ENDHTML;
    
    <FORM METHOD=GET ACTION="http://cs.sunyit.edu/~westj2/cgi-bin/assignment4.pl/page3>
    
    ENDHTML
    
    $elements = $FORM{'numelements'};
    
    for($i = 0; $i <= $elements; $i++)
    {
    	print "Enter number $i: ";
    	print q{<input type="text" name="numelements" maxlength="7"><br>};
    }
    
    print <<ENDHTML;
    <HTML>
    <HEAD>
    <TITLE>Assignment #4</TITLE>
    </HEAD>
    <BODY text="white" bgcolor="blue">
    <FONT face="arial" size="4">
    <DIV align="center">
    <br>
    <input type="submit" name="submit" value="Submit">&nbsp&nbsp
    <input type="reset">
    </FORM>
    </DIV>
    </FONT>
    </BODY>
    </HTML>
    ENDHTML
    }
    
    #if($FORM{'elements'} eq "")
    #{
    #        print "Invalid number of elements!";
    #}
    #else
    #{
    #        &page3
    #}
    
    sub page3
    {
    print <<ENDHTML;
    <HTML>
    <HEAD> 
    <TITLE>Assignment #4</TITLE>
    </HEAD>
    <BODY text="white" bgcolor="blue">
    <FONT face="arial" size="4">
    <DIV align="center">
    <br>
    ENDHTML
    print test;
    }
    So, the problem lies with the error handling. On the first page, I have the user enter the number of elements they want to be added together and averaged. Then, I have some error handling that checks to see if the user entered something. If they didn't, then it displays "Invalid number of elements!". Else, it calls page2. In page2, I have a for loop that displays the number of textboxes needed for the user to enter the elements. Here, when they hit submit after entering in the elements, it shows the "Invalid number of elements!" error again and I don't know why. I'm assuming it has something to do with the previous error handling, but I don't know how to fix it.
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    The strict and warnings pragmas will help you catch some of the obvious and not so obvious problems. I recommend that you start using those.

    Also, where are you declaring the hash %FORM that holds the form data?

    --Kevin

    Comment

    • JWest46088
      New Member
      • Sep 2006
      • 74

      #3
      Originally posted by eWish
      The strict and warnings pragmas will help you catch some of the obvious and not so obvious problems. I recommend that you start using those.

      Also, where are you declaring the hash %FORM that holds the form data?

      --Kevin
      I haven't learned anything about the hash. How exactly would that work?

      I'm having a different problem now with storing the inputs on page2. After the user inputs the numbers and clicks submit, it does the for loop again or something so if the first text box contained the number 3, it will reprint 3 text boxes (while there was 2 before).

      Comment

      • JWest46088
        New Member
        • Sep 2006
        • 74

        #4
        Should I be putting in something like require "subparseform.l ib"; in the code?

        Maybe @elements = values{%FORM}; too?

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Ewish:


          Didn't you notice the URL the form tag points to?

          http://cs.sunyit.edu/~westj2/cgi-bin/assignment4.pl/page2

          ;)

          Comment

          • JWest46088
            New Member
            • Sep 2006
            • 74

            #6
            Originally posted by KevinADC
            Ewish:


            Didn't you notice the URL the form tag points to?

            http://cs.sunyit.edu/~westj2/cgi-bin/assignment4.pl/page2

            ;)
            Where should it be pointed to then? I deleted the /page2 part from all of the form tags and now I'm getting a document not found error.

            Comment

            • JWest46088
              New Member
              • Sep 2006
              • 74

              #7
              I think the main problem lies within the for loop. If I knew how to store the numbers the user inputs into the text boxes on page2, then I would be able to continue on with the rest of the program.

              Comment

              • JWest46088
                New Member
                • Sep 2006
                • 74

                #8
                Originally posted by JWest46088
                I think the main problem lies within the for loop. If I knew how to store the numbers the user inputs into the text boxes on page2, then I would be able to continue on with the rest of the program.
                The user inputs the number of elements to add. The value of the input (from the textbox "numelement s") gets sent to subroutine page2. In sub page2, the user inputs however many numbers they specified in "numelement s". In the for loop to do this, the textbox "numelement s" is printed that many times. How would I change the name of the textbox for all of the textboxes printed out? I think the problem is that the first textbox on page2 overwrites numelements instead of being saved as its own. Then, the for loop takes that input... I don't know.

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Originally posted by JWest46088
                  Where should it be pointed to then? I deleted the /page2 part from all of the form tags and now I'm getting a document not found error.

                  Just letting Ewish know that you are working on school/class/course work. At least you are giving it a try.

                  Comment

                  Working...