On 13/09/2012 23:42, jmrhide-p...@yahoo.com wrote:
>
> I installed the following script last year and it seemed to be working fine.
> Yesterday, however, my hosting service took down my site because the script
> was
> tying up so much of their server resources that it was a threat to their
> business. One of the folks I talked to there said he thought it was starting
> multiple copies of itself that were never terminated. The logs didn't show the
> script being accessed more than a few times a day on average.
>
> I would appreciate help debugging this thing:
Hi John
You have offered us a collapsed building and asked how to restore it to
somewhere fit to live in
Your program has become a monster, and clearly needs a rewrite
You should be able to understand your own software completely, and
explain every line in it, and when and why it is being executed
Instead I think you have chipped away at an existing program, changing
things here and there until it seems to work OK
Doing this again and again has resulted in the code that you have, where
no one knows why and how it works or how to fix it
Assuming that you don't have the means to write a replacement from
scratch, you should start by reformatting the code so that the
indentation is consistent, correct, and readable
Then you should uncomment the `use strict` and `use warnings` lines, and
declare everything at its first point of use
How you proceed beyond that depends on the behaviour of the new program
I can offer you a reformat, courtesy of `perltidy`, that will help you
on your way
HTH,
Rob
#!/usr/local/bin/perl
# use warnings;
# use strict;
# use diagnostics;
use CGI;
my $cgi = new CGI;
my (
$topic, $score, $lastnum, $answer, $anum,
$bnum, $cnum, $dnum, $enum, $smarts,
$playlevel, $c_topic, $c_smarts, $c_playlevel, $c_score,
$c_lastnum, $c_goodans, $stimulus, $texta, $textb,
$textc, $textd, $texte, $defnum, $smiley,
$feedback, $goodans, $restart, @data, @cat,
@term, @def, @story, @candidate, @termnum,
$sound
);
my $instructions = "(Click the button next to the best response)";
my $ops = 0;
use diagnostics;
# GET COOKIES
$topic = $cgi->cookie('topic');
$smarts = $cgi->cookie('smarts');
$playlevel = $cgi->cookie('playlevel');
$score = $cgi->cookie('score');
$lastnum = $cgi->cookie('lastnum');
$goodans = $cgi->cookie('goodans');
# IF NEW CATEGORY THEN (RE)START TUTORIAL, ELSE GET USER ANSWER
if ($cgi->param('Category')) {
$restart = 1;
$topic = $cgi->param('Category');
$smarts = 0;
$playlevel = 0;
$score = 0;
$lastnum = 0;
}
elsif ($cgi->param('Answer')) { $answer = $cgi->param('Answer'); }
elsif ($cgi->param('Next')) { $restart = 1; }
if ($topic) { # RUN TUTORIAL
if ($answer and ($playlevel > 3)) {
# SUBSTITUTE SPACE FOR '+' IN $answer
$_ = $answer;
s/\+/ /g;
$answer = $_;
# RTRIM $answer AND MAKE SURE IT'S ALL LOWER CASE
$answer =~ s/\s+$//;
$answer = lc($answer);
# FIX FRENCH CHARACTERS
if ($answer eq "deja entendu") { $answer = "déjà entendu"; }
elsif ($answer eq "deja vu") { $answer = "déjà vu"; }
elsif ($answer eq "deja vecu") { $answer = "déjà vécu"; }
elsif ($answer eq "folie a deux") { $answer = "folie à deux"; }
elsif ($answer eq "jamais vecu") { $answer = "jamais vécu"; }
elsif ($answer eq "d%e9j%e0 entendu") { $answer = "déjà entendu"; }
elsif ($answer eq "d%e9j%e0 vu") { $answer = "déjà vu"; }
elsif ($answer eq "d%e9j%e0 v%e9cu") { $answer = "déjà vécu"; }
elsif ($answer eq "folie %e0 deux") { $answer = "folie à deux"; }
elsif ($answer eq "jamais v%e9cu") { $answer = "jamais vécu"; }
} # END PLAYLEVEL 4
# LOAD DATA
open FH, "/home1/theinfp0/public_html/psychdef/tutorial.fil" or die $!;
while (<FH>) {
if ($topic eq "REVIEW") { $termnum[$ops++] = $_; }
elsif (/$topic/) { $termnum[$ops++] = $_; }
}
close FH;
$defnum = $ops; # NUMBER OF TERMS IN DATA SET
# PARSE $_ TO GET $term(32) $cat(16) $def(64) $story(128) via @data:
$ops = 0;
foreach (@termnum) {
@data = /(.{16})/g;
$cat[$ops] = $data[0];
$term[$ops] = $data[1] . $data[2];
$def[$ops] = $data[3] . $data[4] . $data[5] . $data[6];
$story[$ops] =
$data[7]
. $data[8]
. $data[9]
. $data[10]
. $data[11]
. $data[12]
. $data[13]
. $data[14];
# RIGHT TRIM STRINGS
$cat[$ops] =~ s/\s+$//;
$term[$ops] =~ s/\s+$//;
$def[$ops] =~ s/\s+$//;
$story[$ops++] =~ s/\s+$//;
}
# EVALUATE RESPONSE AND PROVIDE FEEDBACK, ADJUSTING SCORES
if ($answer and ($answer ne $goodans)) { $answer = 0; }
if ($answer) {
$smarts++;
$score = ++$score + $playlevel;
$smiley = "1"; # SUCCESS
$feedback = "You got it right!";
}
else {
$smarts-- unless $restart;
$feedback = "Better study this!";
}
if ($smarts < -4) { $playlevel--; $smarts = 0; }
elsif ($smarts > 4) { $playlevel++; $smarts = 0; }
# NEXT QUESTION
if ($answer or $restart) { # AVOID CHANGING $lastnum FOR WRONG ANSWER
if ($playlevel < 1) {
while (1) {
$termnum[0] = int(rand($defnum));
last unless ($termnum[0] == $lastnum);
}
$lastnum = $termnum[0];
$stimulus = $term[$termnum[0]];
$candidate[0] = $def[$termnum[0]];
while (1) {
$termnum[1] = int(rand($defnum));
last unless ($termnum[1] == $termnum[0]);
}
$candidate[1] = $def[$termnum[1]];
$anum = int(rand(2));
$bnum = abs($anum - 1);
$texta = $candidate[$anum];
$textb = $candidate[$bnum];
if ($texta eq $candidate[0]) { $goodans = "A"; }
else { $goodans = "B"; }
}
elsif ($playlevel == 1) {
while (1) {
$termnum[0] = int(rand($defnum));
last unless ($termnum[0] == $lastnum);
}
$lastnum = $termnum[0];
$stimulus = $def[$termnum[0]];
$candidate[0] = $term[$termnum[0]];
while (1) {
$termnum[1] = int(rand($defnum));
last unless ($termnum[1] == $termnum[0]);
}
$candidate[1] = $term[$termnum[1]];
while (1) {
$termnum[2] = int(rand($defnum));
last
unless (($termnum[2] == $termnum[1])
|| ($termnum[2] == $termnum[0]));
}
$candidate[2] = $term[$termnum[2]];
$anum = int(rand(3));
if ($anum == 0) { $goodans = "A"; }
while (1) {
$bnum = int(rand(3));
last unless ($bnum == $anum);
}
if ($bnum == 0) { $goodans = "B"; }
$cnum = 3 - ($anum + $bnum);
if ($cnum == 0) { $goodans = "C"; }
$texta = $candidate[$anum];
$textb = $candidate[$bnum];
$textc = $candidate[$cnum];
}
elsif ($playlevel == 2) {
while (1) {
$termnum[0] = int(rand($defnum));
last unless ($termnum[0] == $lastnum);
}
$lastnum = $termnum[0];
$stimulus = $term[$termnum[0]];
$candidate[0] = $def[$termnum[0]];
while (1) {
$termnum[1] = int(rand($defnum));
last unless ($termnum[1] == $termnum[0]);
}
$candidate[1] = $def[$termnum[1]];
while (1) {
$termnum[2] = int(rand($defnum));
last
unless (($termnum[2] == $termnum[1])
|| ($termnum[2] == $termnum[0]));
}
$candidate[2] = $def[$termnum[2]];
while (1) {
$termnum[3] = int(rand($defnum));
last
unless (($termnum[3] == $termnum[2])
|| ($termnum[3] == $termnum[1])
|| ($termnum[3] == $termnum[0]));
}
$candidate[3] = $def[$termnum[3]];
$anum = int(rand(4));
if ($anum == 0) { $goodans = "A"; }
while (1) {
$bnum = int(rand(4));
last unless ($bnum == $anum);
}
if ($bnum == 0) { $goodans = "B"; }
while (1) {
$cnum = int(rand(4));
last unless (($cnum == $bnum) || ($cnum == $anum));
}
if ($cnum == 0) { $goodans = "C"; }
$dnum = 6 - ($anum + $bnum + $cnum);
if ($dnum == 0) { $goodans = "D"; }
$texta = $candidate[$anum];
$textb = $candidate[$bnum];
$textc = $candidate[$cnum];
$textd = $candidate[$dnum];
}
elsif ($playlevel == 3) {
while (1) {
$termnum[0] = int(rand($defnum));
last unless ($termnum[0] == $lastnum);
}
$lastnum = $termnum[0];
$stimulus = $story[$termnum[0]];
$candidate[0] = $term[$termnum[0]];
while (1) {
$termnum[1] = int(rand($defnum));
last unless ($termnum[1] == $termnum[0]);
}
$candidate[1] = $term[$termnum[1]];
while (1) {
$termnum[2] = int(rand($defnum));
last
unless (($termnum[2] == $termnum[1])
|| ($termnum[2] == $termnum[0]));
}
$candidate[2] = $term[$termnum[2]];
while (1) {
$termnum[3] = int(rand($defnum));
last
unless (($termnum[3] == $termnum[2])
|| ($termnum[3] == $termnum[1])
|| ($termnum[3] == $termnum[0]));
}
$candidate[3] = $term[$termnum[3]];
while (1) {
$termnum[4] = int(rand($defnum));
last
unless (($termnum[4] == $termnum[3])
|| ($termnum[4] == $termnum[2])
|| ($termnum[4] == $termnum[1])
|| ($termnum[4] == $termnum[0]));
}
$candidate[4] = $term[$termnum[4]];
$anum = int(rand(5));
if ($anum == 0) { $goodans = "A"; }
while (1) {
$bnum = int(rand(5));
last unless ($bnum == $anum);
}
if ($bnum == 0) { $goodans = "B"; }
while (1) {
$cnum = int(rand(5));
last unless (($cnum == $bnum) || ($cnum == $anum));
}
if ($cnum == 0) { $goodans = "C"; }
while (1) {
$dnum = int(rand(5));
last
unless (($dnum == $cnum)
|| ($dnum == $bnum)
|| ($dnum == $anum));
}
if ($dnum == 0) { $goodans = "D"; }
$enum = 10 - ($anum + $bnum + $cnum + $dnum);
if ($enum == 0) { $goodans = "E"; }
$texta = $candidate[$anum];
$textb = $candidate[$bnum];
$textc = $candidate[$cnum];
$textd = $candidate[$dnum];
$texte = $candidate[$enum];
}
elsif ($playlevel > 3) {
while (1) {
$termnum[0] = int(rand($defnum));
last unless ($termnum[0] == $lastnum);
}
$lastnum = $termnum[0];
$anum = int(rand(2));
if ($anum) { $stimulus = $story[$termnum[0]]; }
else { $stimulus = $def[$termnum[0]]; }
$goodans = $term[$termnum[0]];
$instructions = "Type the correct term, then press SEND!";
}
} # END OF NEW QUESTION
# PRINT RESPONSE:
$c_topic = $cgi->cookie(
-name => 'topic',
-value => $topic,
-expires => '+1d',
-path => '/'
);
$c_score = $cgi->cookie(
-name => 'score',
-value => $score,
-expires => '+1d',
-path => '/'
);
$c_smarts = $cgi->cookie(
-name => 'smarts',
-value => $smarts,
-expires => '+1d',
-path => '/'
);
$c_playlevel = $cgi->cookie(
-name => 'playlevel',
-value => $playlevel,
-expires => '+1d',
-path => '/'
);
$c_lastnum = $cgi->cookie(
-name => 'lastnum',
-value => $lastnum,
-expires => '+1d',
-path => '/'
);
$c_goodans = $cgi->cookie(
-name => 'goodans',
-value => $goodans,
-expires => '+1d',
-path => '/'
);
print "Set-Cookie: $c_topic\n";
print "Set-Cookie: $c_score\n";
print "Set-Cookie: $c_smarts\n";
print "Set-Cookie: $c_playlevel\n";
print "Set-Cookie: $c_lastnum\n";
print "Set-Cookie: $c_goodans\n";
print $cgi->header;
print <<QWERTYUIOP;
http://www.w3.org/TR/REC-html40/loose.dtd\">
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=windows-1252">
<TITLE>PsychDef Tutorial</TITLE>
<META NAME="GENERATOR" CONTENT="OpenOffice.org 3.3,(Win32)">
<META NAME="CREATED" CONTENT="0;0">
<META NAME="CHANGEDBY" CONTENT="John M Rathbun MD">
<META NAME="CHANGED" CONTENT="20110531;20073739">
<META NAME="DESCRIPTION" CONTENT="Psych Glossary Tutorial 254 Behavioral
Health Terms Memorized Easily Painless Learning">
<STYLE TYPE="text/css">
<!--
P { color: #000080 }
TD P { color: #000080 }
H1 { color: #000080 }
A:visited { color: #ff00ff }
-->
</STYLE>
</HEAD>
enctype="application/x-www-form-urlencoded">
<TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0
STYLE="page-break-before:
always">
<COL WIDTH=128*>
<COL WIDTH=128*>
<TR VALIGN=TOP>
<!-- COLUMN ONE STARTS HERE -->
<TD WIDTH=33%>
<P ALIGN=CENTER><BR>
<H1><FONT SIZE=5>Select Category:</FONT></H1>
<P><SELECT NAME="Category" SIZE=10 STYLE="width: 1.43in;
height:
2.33in; font-family: 'Garamond'; font-size: 14pt">
<OPTION VALUE="Consciousness">Consciousness
<OPTION VALUE="Defenses">Defenses
<OPTION VALUE="Emotion">Emotion
<OPTION VALUE="Intellect">Intellect
<OPTION VALUE="Memory">Memory
<OPTION VALUE="Movement">Movement
<OPTION VALUE="Perception">Perception
<OPTION VALUE="Speech">Speech
<OPTION VALUE="Thinking">Thinking
<OPTION VALUE="REVIEW">REVIEW</OPTION>
</SELECT></P>
<DIV ALIGN=LEFT>
</DIV></FORM></TD>
<!-- COLUMN TWO STARTS HERE! -->
<TD WIDTH=67%>
QWERTYUIOP
if ($restart) { # THIS IS EITHER A NEW TOPIC OR FOLLOWS A WRONG ANSWER:
print <<QWERTYUIOP;
<P><BR><BR><BR></P>
<P ALIGN=LEFT><FONT SIZE=3><B>$instructions</B></FONT></P>
<P ALIGN=LEFT><FONT SIZE=4><B>$stimulus</B></FONT></P>
<DIV ALIGN=LEFT>
QWERTYUIOP
if ($playlevel < 4) {
print <<QWERTYUIOP;
$texta
$textb
QWERTYUIOP
if ($textc) {
print <<QWERTYUIOP;
$textc
QWERTYUIOP
}
if ($textd) {
print <<QWERTYUIOP;
$textd
QWERTYUIOP
}
if ($texte) {
print <<QWERTYUIOP;
$texte
QWERTYUIOP
}
} # END PLAYLEVEL < 4
else { # PLAYLEVEL > 3
print <<QWERTYUIOP;
<input type="text" name="Answer" size="32"><br><br>
<input type="submit" value="SEND">
</form>
QWERTYUIOP
} # END PLAYLEVEL > 3
} # END RESTART/RECOVERY
elsif ($smiley) { # CORRECT ANSWER
print <<QWERTYUIOP;
<P><BR><BR><BR></P>
<h1>=>$feedback<=</h1><br>
<h2>Your score is $score</h2>
<P ALIGN=LEFT><FONT SIZE=3><B>$instructions</B></FONT></P>
<P ALIGN=LEFT><FONT SIZE=4><B>$stimulus</B></FONT></P>
<DIV ALIGN=LEFT>
QWERTYUIOP
if ($playlevel < 4) {
print <<QWERTYUIOP;
$texta
$textb
QWERTYUIOP
if ($textc) {
print <<QWERTYUIOP;
$textc
QWERTYUIOP
}
if ($textd) {
print <<QWERTYUIOP;
$textd
QWERTYUIOP
}
if ($texte) {
print <<QWERTYUIOP;
$texte
QWERTYUIOP
}
} # END PLAYLEVEL < 4
else { # PLAYLEVEL > 3
print <<QWERTYUIOP;
<input type="text" name="Answer" size="32"><br><br>
<input type="submit" value="SEND">
</form>
QWERTYUIOP
} # END PLAYLEVEL > 3
} # END CORRECT ANSWER
else { # WRONG ANSWER!
$sound = "$term[$lastnum]";
if ($sound eq "déjà entendu") { $sound = "d%e9j%e0 entendu"; }
elsif ($sound eq "déjà vu") { $sound = "d%e9j%e0 vu"; }
elsif ($sound eq "déjà vécu") { $sound = "d%e9j%e0 v%e9cu"; }
elsif ($sound eq "folie à deux") { $sound = "folie %e0 deux"; }
elsif ($sound eq "jamais vécu") { $sound = "jamais v%e9cu"; }
$sound = $sound . "\.mp3";
print <<QWERTYUIOP;
<P><BR><BR><BR></P>
<h1>=>$feedback<=</h1><br><br>
<H1 CLASS="western">Term: <FONT COLOR="#94006b">$term[$lastnum]</FONT></H1>
<H2 CLASS="western">Category: <FONT COLOR="#94006b"><FONT FACE="Garamond,
serif"><FONT SIZE=5><SPAN STYLE="font-style:
normal">$cat[$lastnum]</SPAN></FONT></FONT></FONT></H2>
<H2 CLASS="western">Definition: <FONT COLOR="#94006b"><FONT FACE="Garamond,
serif"><FONT SIZE=5><SPAN STYLE="font-style:
normal">$def[$lastnum]</SPAN></FONT></FONT></FONT></H2>
<H2 CLASS="western">Vignette: <FONT COLOR="#94006b"><FONT FACE="Garamond,
serif"><FONT SIZE=5><SPAN STYLE="font-style:
normal">$story[$lastnum]</SPAN></FONT></FONT></FONT></H2>
QWERTYUIOP
} # END WRONG ANSWER
print "</DIV></TD></TR></TABLE></BODY></HTML>\n";
} # TUTORIAL ENDS HERE
else { # IN CASE NO COOKIE WAS RECEIVED
print "content-type: text/html\n\n";
print $cgi->start_html('Oops!');
print $cgi->h2(
"OOPS! The PsychDef tutorial must keep track of your progress
so it can adjust the difficulty of the questions you see. It does this by
leaving a small text file known as a 'cookie' on your machine. We didn't
receive
a cookie from your Internet browser, so we can't continue your tutorial."
);
print $cgi->h2(
"Please enable cookies in your browser settings and try
again. If you're not sure how to do this, you can search your browser's 'Help'
index for 'cookies'."
);
print $cgi->h2(
"If you received this message even though your browser allows
cookies, please e-mail 'cookie-monster (at) theinfosite.org'."
);
print $cgi->end_html;
} #END ELSE
# END OF CGI
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/