WeBWorK Main Forum

passing a subroutine reference to withPostFilter

by Michael Shulman -
Number of replies: 3
Sometimes when I try to pass a named subroutine reference to withPostFilter, I get the following error:

...
• Use of uninitialized value $new_rh_ans_evaluation_result in concatenation (.) or string at /opt/webwork/pg/lib/WeBWorK/PG/Translator.pm line 1337 I haven't been able to figure out when this happens and when it doesn't. But it happens with something as simple as this: sub test { return$_[0]; }
ANS( str_cmp( 'FALSE' )->withPostFilter(\&test) );

whereas the following works fine:

sub test { return \$_[0]; }
ANS( str_cmp( 'FALSE' )->withPostFilter(sub { return test(@_); }) );

Is there maybe something about namespacing or evals going on so that the subroutine "test" is visible to the "sub" closure but not via a direct reference?

Re: passing a subroutine reference to withPostFilter

by Michael Gage -
I suspect the problem is   \&test

If you are typing this in a .pg problem file you need to write it as
~~&test

That's because the backslash is reserved for tex commands, of which there
are many, and in order to reference a perl object you use ~~ instead.

(The technical version:  each .pg file is processed to replace all incidences of \ with \\. Then the incidences of ~~ are replaced with \. When this new version of the file is processed the double backslashes are replaced by single backslashes during the interpolation process and then interpreted as TeX commands.

In .pl files for macros (which are not preprocessed) all TeX commands have to be written with double backslashes:  \\par  etc. but \&test would work fine. )

Re: passing a subroutine reference to withPostFilter

by Michael Shulman -
Yikes! So while a .pg file looks like Perl code, it isn't actually. Are there any other gotchas like this?