I am having a vexing problem with eval. Normally in Perl, when you call eval { ... } and the bracketed process dies or fails, the variable $@ is set to the error message. For example, the following command line outputs "XYZ at -e line 1":
perl -e 'eval { die "XYZ" }; $a = $@ // "undefined"; die $a'
I believe that this is standard Perl behavior. The webwork library also uses this feature of eval, for example in pg/lib/Value/AnswerChecker.pm.
On the other hand, when I try this in a WebWork problem or macro, it does not work, and it is vexing me. I cannot figure out why this is not working. For example, the following test problem outputs "Debug: undefined", rather than "Debug: XYZ" as it should.
DOCUMENT();
loadMacros(
"PGbasicmacros.pl",
);
TEXT(beginproblem());
eval { die "XYZ" };
$a = $@ // "undefined";
BEGIN_TEXT
Debug: $a
END_TEXT
ENDDOCUMENT();
Does someone know why? I am trying to program a new kind of answer checker, and I am trying to replicate what I found in pg/lib/Value/AnswerChecker.pm. Everything works more or less fine, except I cannot get $@ to hold the error message. I thought maybe it has something to do with the PG preprocessor, but I don't think this is the case. Help?
Thanks! -- Peter