WeBWorK Main Forum

integrating perl module

integrating perl module

by Zak Zarychta -
Number of replies: 2

Following the instructions from a post by Maria Voloshina here, I've attempted to add the perl module Math::SigFigs to our locally hosted WeBWorK.

I've added the renamed perl module located at /webwork/pg/lib/SigFigs.pm to webwork/pg/macros/PG_module_list.pl and also created a file in the same directory PGSigFigsmacros.pl which contains the line

sub _PGSigFigsmacros_init {
foreach my $t (@SigFigs::EXPORT_OK) { *{$t} = *{"SigFigs::$t"} }
}

The MWE included below loads the macro file I created in line with the above advice just fine. However, it throws an exception when I try to call the function addSF from the renamed perl module located at /webwork/pg/lib/SigFigs.pm with the following error 

1. ERROR caught by Translator while processing problem file:setSANDPIT/SigFig_00-0.pg
****************
ERRORS from evaluating PG file: 
 Undefined subroutine &main::addSF called at line 13 of (eval 1715)
The 
I'd be very grateful for advice that will get me over the line with this.

Zak

####################################

DOCUMENT();

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

TEXT(beginproblem());
Context("Numeric");
$n1 = 123.4567898765;
$n2 = 987.654321;

$sum12 = addSF($n1,$n2);
#$sum12 = $n1 + $n2;
$ans = $sum12;

ANS( Real($ans)->cmp());

Context()->texStrings;
BEGIN_TEXT
$BR
\(n1 + n2 = $n1 + $n2\)
$BR
\(\phantom{n1 + n2} = $sum12\)
$PAR
\{ ans_rule(25) \}
END_TEXT
Context()->normalStrings;

$showPartialCorrectAnswers = 1;
ENDDOCUMENT();

##################################

In reply to Zak Zarychta

Re: integrating perl module

by Glenn Rice -

The post you are referring to is long outdated.  To add modules to PG (which is not something that you generally should do unless you really know what you are doing) you need to add the module to the modules list in /opt/webwork/webwork2/conf/localOverrides.conf.  There is an example in the file on how to do this in the file that reads

push (@{${pg}{modules}}, [qw(LaTeXImage)]);
So for the module Math::SigFigs you would add

push (@{${pg}{modules}}, [qw(Math::SigFigs)]);

The Perl module file Math/SigFigs.pm should not be copied anywhere.  Leave that were it is installed for the system.

In reply to Glenn Rice

Re: integrating perl module

by Zak Zarychta -

Glen,
many thanks, your fix worked a treat

The only thing to remember is that the full namespace of the module needs to be prepended to the function call so

#$sum12 =  addSF($n1,$n2); #needs to be written as below
$sum12 =  Math::SigFigs::addSF($n1,$n2);

Zak