#!/usr/local/bin/perl -- -*- C -*- use CGI; $query = new CGI; print $query->header; print $query->start_html("Perl5q1.pl"); print $query->start_form; $ans = 5280; print "<P> How many feet are there in a mile?", print "<P>What's your answer? Choose your answer in scientific notation, i.e., <br>±x.yzE{±ab}, and then press the submit query button.<P>", $query->scrolling_list('s1', ['+','-'],'+'), $query->popup_menu('x', ['1','2','3','4','5','6','7','8','9'],'1'), "<STRONG>.</STRONG> ", $query->popup_menu('y', ['0','1','2','3','4','5','6','7','8','9'],'0'), $query->popup_menu('z', ['0','1','2','3','4','5','6','7','8','9'],'0'), "<STRONG>E{</STRONG>", $query->scrolling_list('s2', ['+','-',],'+'), $query->popup_menu('xx', ['0','1','2','3','4','5','6','7','8','9'],'0'), $query->popup_menu('yy', ['0','1','2','3','4','5','6','7','8','9'],'0'), "<STRONG>}</STRONG>"; print "<p>Query, is this answer correct?",$query->submit; print $query->end_form; print "<HR> "; if ($query->param) { print "<P><EM>Your answer, ", $query->param('s1'), $query->param('x'),".", $query->param('y'), $query->param('z'),"E{", $query->param('s2'), $query->param('xx'), $query->param('yy'),"}","</EM> "; $stu_ans = $query->param('x')+(10*$query->param('y')+$query->param('z'))/100; if ($query->param('s1') ne "+"){$stu_ans = - $stu_ans}; $power = $query->param('xx')*10+$query->param('yy'); if ($query->param('s2') ne "+"){$power = - $power}; $stu_ans = $stu_ans * (10 ** $power); $v = (($stu_ans - $ans)**2 ); #print "<br> DEBUG **** $ans and $stu_ans and $v"; if ( $v - (0.01*(10**$power))**2 >= 0.0 ) {print "<EM>, was Wrong</EM>. <IMG SRC=../../icons/checkno.gif>"} else {print "<EM>, was Right! </EM>. <IMG SRC=../../icons/check.gif>" }; }
[1 ]#!/usr/local/bin/perl -- -*- C -*- [2 ]use CGI; [3 ]$query = new CGI; [4 ]print $query->header; [5 ]print $query->start_html("Perl5q1.pl"); [6 ]print $query->start_form; [7 ]$ans = 5280; [8 ]print "<P> How many feet are there in a mile?",Lines [1] thru [8] are identical to the original question, and are discussed there:Click Here to See Original Prototype
[9 ]print "<P>What's your answer? Choose your answer in scientific notation, [10]i.e., <br>±x.yzE{±ab}, and then press the submit query button.<P>", [11] $query->scrolling_list('s1', [12] ['+','-'],'+'), [13] $query->popup_menu('x', [14] ['1','2','3','4','5','6','7','8','9'],'1'), [15] "<STRONG>.</STRONG> ", [16] $query->popup_menu('y', [17] ['0','1','2','3','4','5','6','7','8','9'],'0'), [18] $query->popup_menu('z', [19] ['0','1','2','3','4','5','6','7','8','9'],'0'), [20] "<STRONG>E{</STRONG>", [21] $query->scrolling_list('s2', [22] ['+','-',],'+'), [23] $query->popup_menu('xx', [24] ['0','1','2','3','4','5','6','7','8','9'],'0'), [25] $query->popup_menu('yy', [26] ['0','1','2','3','4','5','6','7','8','9'],'0'), [27] "<STRONG>}</STRONG>";Lines [9] thru [27] constitute one `line' of Perl code. Line [9] and [10] are textual in nature, telling the student that we are requiring a floating point answer in computer form, i.e., sign, digit, decimal point, two digits followed by the power for ten (written E from old FORTRAN days), where the power of ten constitutes a sign and two digits. Each numerical item is a popup_menu, ranging from 0 to 9, allowing a single choice of a single digit (except the first, which forbids the choice of zero); each sign is a scolling_list with two items.
[28]print "<p>Query, is this answer correct?",$query->submit;Perhaps we should have included a reset button in this example (I forgot). Reset would have changed all the signs back to `+' and all the choices to the defaults, i.e., +1.00E+00. Sorry about that.
[29]print $query->end_form; [30]print "<HR> ";Line [30] tells the computer to emit the HTML code for signalling a horizontal rule, i.e., a divider line between the question (above) and our remarks (coming up) below.
Lines [39] thru [53] constitute the outer if statement, contingent on the student making some answer. If the student just hits the submit button, nothing will happen, as this `parameter' will be false.
[31]if ($query->param) { [32] print "<P><EM>Your answer, ", [33] $query->param('s1'), [34] $query->param('x'),".", [35] $query->param('y'), [36] $query->param('z'),"E{", [37] $query->param('s2'), [38] $query->param('xx'), [39] $query->param('yy'),"}","</EM> ";Lines [32] thru [39] constitute one Perl statement (printed on separate lines for clarity!), which echo the student's response back in student readable form.
[40]$stu_ans = $query->param('x')+(10*$query->param('y')+$query->param('z'))/100;x.yz means that y is in the tenth place, and z is in the hundredth place, so Line [40] creates a number from x, y, and z.
[41]if ($query->param('s1') ne "+"){$stu_ans = - $stu_ans};Line [41] makes the emerging x.yz negative, -x.yz on the basis of the value of s1, i.e., if s1 is not positive (i.e., the student chose negative) then change the sign.
[42]$power = $query->param('xx')*10+$query->param('yy'); [43]if ($query->param('s2') ne "+"){$power = - $power};Lines [42] and [43] do the same thing with with the power choice.
[44]$stu_ans = $stu_ans * (10 ** $power);Line [44] takes x.yy which is in $stu_ans, multiplies it by the appropriate power, and then stores the answer in $stu_ans.
[45]$v = (($stu_ans - $ans)**2 );Line [45] forms the square of the difference between the `correct' answer and the student answer.
[46]#print "<br> DEBUG **** $ans and $stu_ans and $v";Line [46] is a debugging comment, i.e., omitting the pound sign lets this line operate (and print out) while as shown, the line is silent.
[47]if ( $v - (0.01*(10**$power))**2 >= 0.0 )Line [47] sets the tolerance of $v, i.e., trying to pick out the hundredth digit in scientific notation.
[48] {print "<EM>, was Wrong</EM>. [49] <IMG SRC=../../icons/checkno.gif>"} [50] else {print "<EM>, was Right! </EM>. [51] <IMG SRC=../../icons/check.gif>" [52]}; [53]}Notice that we can be a bit sloppy, i.e., no end_form is used, and still all of this executes properly.
http://www.sp.uconn.edu/~cdavid/cgi-bin/perl5q1.plas an example, and then we edit the location to insert cgitap between cgi-bin and perl5q1.pl thusly:
http://www.sp.uconn.edu/~cdavid/cgi-bin/cgitap/perl5q1.plwhich can be seen here. You can see the documentation for this (CGITAP) program here. This program is incredible, showing the HTML which creates the page each time it is either loaded or submitted. Wow.