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

Code debugging - Web survey

Kelicula
Expert 100+
P: 176
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
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
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
Expert 2.5K+
P: 4,059
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
Expert 100+
P: 176
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

Post your reply

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