473,325 Members | 2,774 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,325 software developers and data experts.

Code debugging - Web survey

Kelicula
176 Expert 100+
Hello all! I am getting very close to comleting my recent project thanks to everyones help.

I am at a stage where I will start to emplimement end result code. (almost)

But I have a problem. For some reason, unbeknownst to me (is that a word?)
It doesn't work!!!
Here is the code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl -w
  2.  
  3. use CGI qw(:standard);
  4. use Tie::File;
  5.  
  6. sub irQ{
  7.     print <<EOM;
  8.  
  9.  
  10. <h1>Thanks for voting!</h1>
  11.  
  12. Here are your results:<a href="../results.txt">Click Here</a>
  13.  
  14. EOM
  15. }
  16.  
  17. sub inK{
  18.     print <<EOM;
  19.  
  20. <h2>You must wait 24 hours between each vote. </h2>
  21.  
  22. Please try again later.
  23. Check out the articles <a href="http://www.liveaudiomag.com">Here</a>
  24.  
  25. EOM
  26. }
  27.  
  28. $tf = "true";
  29. $canI = "false";
  30. $ip = param(ip);
  31. $choice = param(choice);
  32. $time = time;
  33.  
  34. tie @res, 'Tie::File', "../results.txt" or die;
  35.  
  36. foreach $i (@res) {        # Loop through the file, looking for matching ip address.
  37.  
  38.     if ($i =~ m/$ip/) {        # If ip matches 
  39.  
  40.         $get = substr($i, 0, 10);    # Get the last time associated with this ip.
  41.  
  42.         if ($get+86400 < $time) {    # If time is less than 24hrs ago
  43.             $canI = "false";            # no push to array
  44.             $tf = "false";                # take to can't vote page.
  45.  
  46.         } else {                     # If time is greater than 24hrs. 
  47.             $i =~ s/$get/$time/;        # update time, for next time they vote.
  48.             $canI = "false";            # No push to array (already have their vote)
  49.             $tf = "true";                # take to thanks for voting page.
  50.         }
  51.  
  52.     } else {                # If no ip match
  53.         $canI = "true";            # do push to array
  54.         $tf = "true";            # take to thanks for voting page.
  55.     }
  56. }
  57.  
  58. if ("$canI" eq "true"){            # to push or not to push...
  59.     push(@res, ("$time:$ip", "$choice"));
  60. }
  61.  
  62. untie @res;
  63.  
  64. print qq{Content-type: text/html\n\n};
  65. print qq{<html><head><title>Thanks for voting! | LiveAudioMag.com</title></head>\n};
  66. print qq{<body bgcolor='#000000' text='1cd90b'>\n};
  67.  
  68.  
  69. if ("$tf" eq "true") {    # Which page to go to?
  70.     irQ();                # Thanks for voting
  71. } else {
  72.     inK();                # Can't vote page.
  73. }
  74.  
  75.  
  76. print end_html();
  77.  

Notice the comments. It seems very straight forward.
Everything is spelled out very plain.

But it DOESN"T work. ???

Here is the URL.

Liveaudiomag survey.html

Anyone see where I am going wrong?
Aug 26 '07 #1
3 1484
KevinADC
4,059 Expert 2GB
You need to redo the script (not entirely), luckily it is a short script at ths point.

First and foremost is:

Expand|Select|Wrap|Line Numbers
  1. use strict;
Second is don't use the -w switch on the shebang line, use the "warnings" pragma instead:

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2.  
So your script should look like this at the beginning:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5. use CGI qw(:standard);
  6. use Tie::File;
Thirdly, this is eventually going to get you in trouble:

if ("$canI" eq "true"){

do not quote scalars unless you intend to make a string out of them or use them in a string. Much better written like this:

if ($canI eq 'true'){

quotes (single and double) can hide errors in your code that "warnings" and "strict" can't even help you to find. For example:

if ("canI" eq "true"){

You could stare at your code for hours and not spot that error and your code would run and not return any errors or warnings. If you think you would never make an error like that you just haven't written enough perl code yet or you're insane (just a joke mate).

Forth: You wisely test for system success/failure with "die" but then fail to print out the reason the expression died:

tie @res, 'Tie::File', "../results.txt" or die "Can't Tie::File "../results.txt: $!";

Fifth: saying "it doesn't work" is no help to us. What does it do? What doesn't it do? We need details and any error messages.

This is odd:

Here are your results:<a href="../results.txt">Click Here</a>

Normally hyper links do not point to .txt files. But that is something you can fix later.
Aug 26 '07 #2
KevinADC
4,059 Expert 2GB
You need to rework the script, luckily it is a short script at ths point.

First and foremost is:

Expand|Select|Wrap|Line Numbers
  1. use strict;
Second is don't use the -w switch on the shebang line, use the "warnings" pragma instead:

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2.  
So your script should look like this at the beginning:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5. use CGI qw(:standard);
  6. use Tie::File;
Thirdly, this is eventually going to get you in trouble:

if ("$canI" eq "true"){

do not quote scalars unless you intend to make a string out of them or use them in a string. Much better written like this:

if ($canI eq 'true'){

quotes (single and double) can hide errors in your code that "warnings" and "strict" can't even help you to find. For example:

if ("canI" eq "true"){

You could stare at your code for hours and not spot that error and your code would run and not return any errors or warnings. If you think you would never make an error like that you just haven't written enough perl code yet or you're insane (just a joke mate).

Forth: You wisely test for system success/failure with "die" but then fail to print out the reason the expression died:

tie @res, 'Tie::File', "../results.txt" or die "Can't Tie::File "../results.txt: $!";

Fifth: saying "it doesn't work" is no help to us. What does it do? What doesn't it do? We need details and any error messages.

This is odd:

Here are your results:<a href="../results.txt">Click Here</a>

Normally hyper links do not point to .txt files. But that is something you can fix later.
Aug 26 '07 #3
Kelicula
176 Expert 100+
I have taken your advice about the double quoted strings.
Also, the results.txt was just a way for me to "check on what's happening with the file" it won't be like that once completed.

I figured out what was wrong.
I had a superflous else statment that was resetting my canI, and tf variables on subsequent lines after the match was found. I removed it and now it works!

I also uncluded the flock() function.

About the errors. The only thing I ever get when it doesn't work is error 500 internal server error. That is very frustrating. I never know why it doesn't work?!

Well all I have to do now is, make the stats page. I'm planning on searching the file (with Tie::File) and getting a count for each of the choices entered, then translating them into percentages. It shouldn't take too long.
I acually did it!! With alot of help.
Kevin, I can't thank you enough. Everyone has been very generous. I have a long way to go with PERL, and I knew almost nothing at the beginning of this.
Thanks to all!!

Check out the updated version. If ya want.
It works now!
Live audio mag survey
Aug 26 '07 #4

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

Similar topics

1
by: GTF | last post by:
PHP Web Survey Idea.. I have been given an opportunity to create a web based survey. This is a fairly lengthy survey of 60 pages on paper (various multiple choice and free form). These are...
4
by: techy techno | last post by:
Hii I am trying to write down a cdont aplication which will email the survey form to the TO address with the answers checked by the user using the survey form I am 100% sure that there is...
8
by: S.Marion | last post by:
Hello, We are interested in programmers' experiences of programming languages supported by managed runtimes (including but not limited to Java, C#, etc). In particular, we are interested in bugs...
0
by: Kenzo Fong | last post by:
Hi everyone, Sorry to fill up this newsgroup with this request, but for coursework at Erasmus University (the Netherlands) I need to conduct a survey regarding the use of certain open source...
1
by: Nico Baumgarten | last post by:
Dear Madam/Sir, You are invited to participate in an international research study. This research project is headed and led by Cambridge student Nico Baumgarten. What is it all about? It is a...
0
by: Jeff Rush | last post by:
In working up a response to the survey being conducted by Forrester Research on dynamic languages, there is a section wherein they want to see code samples. The samples must include all code...
7
by: martin DH | last post by:
Hello, I have a report that I open that pull its data from a form that builds a where string. Opening the report first opens the form, where I enter criteria, and then pulls matching records from a...
0
by: Janet93 | last post by:
If you are involved in the development of scientific computing software, you are invited to participate in a survey on developing this kind of software. If you have already received this request, I...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.