There are several problems with the code fragment you have given.
First, your correct answer is a plain perl number, not a perl object, and in particular, not a MathObject. You should use
$correct = Real(5);
for that (being sure to have loaded
MathObjects.pl in your
loadMacros() call at the top of the file).
Next, for a MathObject, you don't use
num_cmp or
fun_cmp or any of the older answer checkers directly. Instead, you use the
cmp method of the MathObject, since it knows its own type, and can tell what type of answer checking you need:
$correct->cmp(...);
Next, the return value for your checker routine should be 0 or 1, depending on whether the student's answer is correct or not. You seem to be returning the student's answer instead. (Technically, you can give partial credit by giving a decimal value between 0 and 1. I don't remember wether the return value is clipped to that range, but it might be that returning -5 would take AWAY points.)
Finally, that cmp method returns an AnswerEvaluator object, and that needs to be passed to ANS() to make it active. The usual method would be to do something like
ANS($correct->cmp(...));
So for your case, something like:
ANS(Real(5)->cmp(checker => sub {
my ($correct,$student) = @_;
return $correct == $student || $correct == -$student;
}));
should do the trick.
There are a number of samples in this discussion forum. See, for example:
https://webwork.maa.org/moodle/mod/forum/discuss.php?d=5637
https://webwork.maa.org/moodle/mod/forum/discuss.php?d=4308
https://webwork.maa.org/moodle/mod/forum/discuss.php?d=5421
https://webwork.maa.org/moodle/mod/forum/discuss.php?d=5736
You can also look at the
pg/macros/answerCustom.pl file for some examples and documentation. See the
PG file POD documentation for formatted versions of the comments within the files. There are some sections on MathObjects, for example, that will give you a start (though these files are woefully incomplete). The
answerCustom.pl file is documented there, as are all the main PG macro files.
Hope that helps.
Davide