473,395 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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!

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
8 1761
eWish
971 Expert 512MB
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
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
Should I be putting in something like require "subparseform.lib"; in the code?

Maybe @elements = values{%FORM}; too?
Feb 15 '08 #4
KevinADC
4,059 Expert 2GB
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
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
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
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
4,059 Expert 2GB
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

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

Similar topics

12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
2
by: Julia Baresch | last post by:
Hi everyone, As some of you may know, we've been having trouble with an unrecognized database format error. Today I installed an unfinished project on the workstation of one of my users. ...
6
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
7
by: Spacen Jasset | last post by:
The main two desirable things I feel error handling should provide are these: 1) Debugging and diagnostic aid 2) User feedback One method that is used a fair amount it to 'say' that all...
5
by: tiger79 | last post by:
Hello, I'd like to know what the C# counterpart is for the VB On Error statement ? My VB code looks like this : On Error GoTo ErrGetItem plain simple ud say, but I need to "translate" it into...
5
by: adam.timberlake | last post by:
I've just finished reading the article below which goes into some depth about exceptions. The article was rather lucid and so I understand how to implement it all, the thing I'm having trouble with...
1
by: sean_walsh | last post by:
Hi From classic ASP, I had a custom error handling situation that was quite simple. Errors were all redirected to Error.asp. This page would check 2 settings, EmailErrorMessage and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.