By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Error handling won't go away!

P: 74
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!

Expand|Select|Wrap|Line Numbers
  1. #! /usr/local/bin/perl
  2.  
  3. BEGIN
  4. {
  5.     open(STDERR, ">&STDOUT");
  6.     select(STDERR); $| = 1;
  7.     select(STDOUT); $| = 1;
  8.     print "Content-type: text/html\n\n";
  9. }
  10.  
  11. if($ENV{'QUERY_STRING'} eq "")
  12. {
  13.     &page1;
  14.     exit;
  15. }
  16.  
  17. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  18.  
  19. if($buffer eq "")
  20. {
  21.     $buffer = $ENV{'QUERY_STRING'};
  22. }
  23.  
  24. @pairs = split(/&/, $buffer);
  25. foreach $pair(@pairs)
  26. {
  27.         ($name, $value) = split(/=/, $pair);
  28.  
  29.     $value =~ tr/+/ /;
  30.         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  31.         $name =~ tr/+/ /;
  32.         $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  33.  
  34.     if($FORM{$name} eq "")
  35.     {
  36.         $FORM{$name} = $value;
  37.     }
  38.     else
  39.     {
  40.         $FORM{$name} .= " " . $value;
  41.     }
  42. }
  43.  
  44. print <<ENDHTML;
  45. <HTML>
  46. <HEAD>
  47. <TITLE>Assignment #4</TITLE>
  48. </HEAD>
  49. <BODY text="white" bgcolor="blue">
  50. <FONT face="arial" size="4">
  51. <DIV align="center">
  52. <br>
  53. ENDHTML
  54.  
  55. sub page1
  56. {
  57. print <<ENDHTML;
  58. <HTML>
  59. <HEAD>
  60. <TITLE>Assignment #4</TITLE>
  61. </HEAD>
  62. <BODY text="white" bgcolor="blue">
  63. <FONT face="arial" size="4">
  64. <DIV align="center">
  65. <br>
  66.  
  67. <FORM METHOD=GET ACTION="http://cs.sunyit.edu/~westj2/cgi-bin/assignment4.pl/page2">
  68.  
  69. Enter number of elements to add and average:&nbsp
  70. <input type="text" name="numelements" value="" maxlength="7"><br><br>
  71.  
  72. <input type="submit" name="submit" value="Submit">&nbsp&nbsp
  73. <input type="reset">
  74. </FORM>
  75. </DIV>
  76. </FONT>
  77. </BODY>
  78. </HTML>
  79.  
  80. ENDHTML
  81. }        
  82.  
  83. if($FORM{'numelements'} eq "")
  84. {
  85.     print "Invalid number of elements!";    
  86. }
  87. else
  88. {
  89.     &page2;
  90. }
  91.  
  92. sub page2
  93. {
  94. print <<ENDHTML;
  95.  
  96. <FORM METHOD=GET ACTION="http://cs.sunyit.edu/~westj2/cgi-bin/assignment4.pl/page3>
  97.  
  98. ENDHTML
  99.  
  100. $elements = $FORM{'numelements'};
  101.  
  102. for($i = 0; $i <= $elements; $i++)
  103. {
  104.     print "Enter number $i: ";
  105.     print q{<input type="text" name="numelements" maxlength="7"><br>};
  106. }
  107.  
  108. print <<ENDHTML;
  109. <HTML>
  110. <HEAD>
  111. <TITLE>Assignment #4</TITLE>
  112. </HEAD>
  113. <BODY text="white" bgcolor="blue">
  114. <FONT face="arial" size="4">
  115. <DIV align="center">
  116. <br>
  117. <input type="submit" name="submit" value="Submit">&nbsp&nbsp
  118. <input type="reset">
  119. </FORM>
  120. </DIV>
  121. </FONT>
  122. </BODY>
  123. </HTML>
  124. ENDHTML
  125. }
  126.  
  127. #if($FORM{'elements'} eq "")
  128. #{
  129. #        print "Invalid number of elements!";
  130. #}
  131. #else
  132. #{
  133. #        &page3
  134. #}
  135.  
  136. sub page3
  137. {
  138. print <<ENDHTML;
  139. <HTML>
  140. <HEAD> 
  141. <TITLE>Assignment #4</TITLE>
  142. </HEAD>
  143. <BODY text="white" bgcolor="blue">
  144. <FONT face="arial" size="4">
  145. <DIV align="center">
  146. <br>
  147. ENDHTML
  148. print test;
  149. }
  150.  
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.
Feb 15 '08 #1
Share this Question
Share on Google+
8 Replies


eWish
Expert 100+
P: 971
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
Feb 15 '08 #2

P: 74
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).
Feb 15 '08 #3

P: 74
Should I be putting in something like require "subparseform.lib"; in the code?

Maybe @elements = values{%FORM}; too?
Feb 15 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
Ewish:


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

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

;)
Feb 15 '08 #5

P: 74
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.
Feb 15 '08 #6

P: 74
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.
Feb 15 '08 #7

P: 74
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 "numelements") gets sent to subroutine page2. In sub page2, the user inputs however many numbers they specified in "numelements". In the for loop to do this, the textbox "numelements" 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.
Feb 15 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
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.
Feb 15 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.