Equation Input

In order to extend Computer Assisted Testing beyond simple numberical responses, we need to be able in input equations. For simple equations, a linear notation based on TeX has been adopted.
The equation is inputted by the student into a standard textfield, just the same way we did numbers.
#!/usr/local/bin/perl -- -*- C -*-
use CGI;
require "./query_2dig.pl";
$query = new CGI;
print $query->header;

print $query->start_form;
print "<a href = notation.pl>Click Here for Notation Help</A>";
print "<br><a href=../../send_memo.html>Click here to send a message concerning errors in this question.<br></a>";

srand;
$p = rand;
$V = rand;
print "
<br>
Find the equation of a straight line which passes through the point p=2, V=-1,
and has a slope (p = ordinate(vertical), V = abscissa(horizontal)) of -4/5.
Give your answer in the form 'p = ' as shown, i.e., do not include either 'p'
or '='.
";
$ans =  "(-4/5)*V+6/5";
print "<p> p = ",$query->textfield('ans','',30,100), " Equation for p(V).";

print "<br> Query, is the above answer correct?",$query->submit;
print "<br>",$query->reset;

print $query->end_form;


$returned_ans = $query->param('ans');
$safety_ans = $returned_ans;
if ( $query->param('ans') ne '')
{
$returned_ans =~ s/\//gi; 
$returned_ans =~ s/\$//gi; 
$returned_ans =~ s/#//gi; 
$returned_ans =~ s/~//gi; 
$returned_ans =~ s/V/$V/gi; 
$returned_ans =~ s/p/$p/gi; 
$ans =~ s/V/$V/gi; 
$ans =~ s/p/$p/gi; 
print "student_answer = $safety_ans";
# check for only digits here, D or *,/,(, etc., before allowing eval
$stu_ans = eval($returned_ans);
$ans =  eval($ans);
#print "<p> student_answer  = $returned_ans <p>";
#print "student_answer evaluated = $stu_ans<p>";
#print "answer evaluated = $ans <p>";
if ( (($stu_ans - $ans)**2 - 0.0001  ) ge 0.0 ) 
	{print ", <EM> Wrong</EM>,
	<IMG SRC=../../icons/checkno.gif>"}
 else {print ", <EM> Right! </EM>,
	<IMG SRC=../../icons/check.gif>" };
};

print $query->end_html;
Then the equation is cleaned up. First, offensive characters are removed so that hackers loose their freedom to interfere with our work.
$returned_ans =~ s/\//gi; 
$returned_ans =~ s/\$//gi; 
$returned_ans =~ s/#//gi; 
$returned_ans =~ s/~//gi; 
Then the real work begins, we substitute into both the answer string and the student answer string random numbers for each variable and symbolic constant, and evaluate the resultant algebraic expression.
$returned_ans =~ s/V/$V/gi; 
$returned_ans =~ s/p/$p/gi; 
$ans =~ s/V/$V/gi; 
$ans =~ s/p/$p/gi; 
$V and $p were assign to random numbers:
$p = rand;
$V = rand;
at the beginning, so we use these random numbers in both ours and the student's equation. The results of subsitution and evaluation should be identical if the two expressions are algebraically equivalent! That's all there is to it.
Here is an example of the above code which shows how it works.