WeBWorK Problems

Subroutine return type in custom answer checker

Subroutine return type in custom answer checker

by Steven Fiedler -
Number of replies: 1

I would appreciate learning more about the return types for subroutines as used within the custom answer checker.  Below is code that was distilled from the bottom example on the wiki for the Custom Answer Checker page. The simple "override" subroutine does not appear to have any effect on the answer checker.  The nested (wrap) routine on the other hand, behaves in the expected manner by permitting other answers, say 4.  For clarity, it would be nice to cast the return value in the simple "override" routine to permit its direct use. 

DOCUMENT();
loadMacros( "PGstandard.pl", "MathObjects.pl");

   $val=Real(3);
 
   BEGIN_TEXT
      Enter the number 3:   \{ans_rule(15)\}
   END_TEXT
 
   ANS($val->cmp(checker => override() ));
#  ANS($val->cmp(checker => wrap() ));

   sub override { return 1; }

   sub wrap {
        return sub { return override(); };
   }

ENDDOCUMENT();



In reply to Steven Fiedler

Re: Subroutine return type in custom answer checker

by Davide Cervone -
The problem is that you are not passing the subroutine as the checker, but the return value of the subroutine when called as override(). You need to do either

sub override {return 1}

ANS($val->cmp(checker => ~~&override ));
or

$override = sub {return 1};

ANS($val->cmp(checker => $override ));
or

ANS($val->cmp(checker => sub {
  return 1;
} ));
Many people prefer the latter if they are only using the custom checker once, otherwise, the middle one is good if you are defining the subroutine in a macro file.