WeBWorK Main Forum

Which perl function from which macro file?

Which perl function from which macro file?

by Andrew Dabrowski -
Number of replies: 8
Is there an easy way to look up which .pl file to load in order to make a certain perl utility available?

I was stiuck for a while trying to use List::Util "max", and eventually discovered it's made available by loading PGauxiliaryFunctions.pl.

Now I would like to use perl's List::Util sum and I'm tired of googling.
In reply to Andrew Dabrowski

Re: Which perl function from which macro file?

by Michael Gage -
http://webwork.maa.org/moodle/mod/data/view.php?id=5

is probably what you are looking for.  It has not been updated recently but it will find most subroutines available in the PG safe compartment. 

Importing a new CPAN module into the PG safe compartment is a bigger deal and takes more work.  It has to be listed in the appropriate list in defaults.config. (You'll see where the the CPAN modules are loaded).  I think you found the local version of "max" not the one from List::Utils

I don't think there is a "sum" subroutine but it is pretty easy to implement yourself.  You can place new subroutines in a  macro file  such as mymacros.pl 
and place this in myCourse/templates/macros  and reference it from the .pg
files that you write. 

Hope this helps.

Mike
In reply to Michael Gage

Re: Which perl function from which macro file?

by Andrew Dabrowski -
Perl 5.20.2 has it, the command
use List::Util "sum"
loads a working "sum" command.

In defaults.conf, do you mean section starting here?
${pg}{modules} = [
 [qw(HTML::Parser)],
 [qw(HTML::Entities)],
 [qw(DynaLoader)],

I added a line "[qw(List::Util::sum)]", also tried "[qw(List::Util]", but neither worked. Would it be necessary to call "sum" using a namespace prefix of some kind?


Now that you mention it, I see that PGauxiliaryFunctions.pl does just reimplement some utilities from List::Util. In these (and my) case that's easy to do, but I'd rather learn how to avoid reinventing the wheel.

 

In reply to Michael Gage

Re: Which perl function from which macro file?

by Andrew Dabrowski -
Thanks for that link, just what I was looking for. I had stumbled across it before but couldn't locate it when I needed it.
In reply to Michael Gage

Re: Which perl function from which macro file?

by Andrew Dabrowski -
A good example of a handy perl function I wouldn't want to reimplement myself from scratch is Data::Dumper. I'd love to get that working.
In reply to Andrew Dabrowski

Re: Which perl function from which macro file?

by Michael Gage -
[ qw(List::Util) ],

Should do it.  You'll need to restart.
However I would advise against doing it, particularly for something simple like
sum or max.

The .pg and macros problems are run inside a Safe.pm compartment to keep code in one the problems from inadvertently (or maliciously) doing something like erasing the hard drive.  Basically all functions that print or have access to the hard drive are passed through PG functions that limit their abilities (and could limit them more if we find an exploit).  So far there have been, as far as we know, no security breaches through WeBWorK. Probably mostly because people aren't trying very hard, but we want to protect things as much as we can.

Think whether you really need Data::Dumper inside the Safe compartment that renders the .pg problem.  Often there is another way.

Data::Dumper is available outside in the webwork2 directory that handles that LMS portion of WeBWorK but that portion doesn't use lots of evals to evaluate outside code.

-- Mike
 


In reply to Michael Gage

Re: Which perl function from which macro file?

by Andrew Dabrowski -
You mean restart the http server? I did that. Also tried making Data::Dumper available in the same way, that also failed.

Are sum and max really potentially unsafe? More so than the thousand other functions that are included in the safe compartment?

Moreover, I would only be running Dumper on my office computer for the purpose of writing problems, it would not be on the actual server.

The following statement is cryptic to me.

"Data::Dumper is available outside in the webwork2 directory that handles that LMS portion of WeBWorK but that portion doesn't use lots of evals to evaluate outside code."

Could you expand? Is there an easy way to make Dumper available?
In reply to Andrew Dabrowski

Re: Which perl function from which macro file?

by Michael Gage -
All of the code in the webwork2 directory  runs outside the Safe compartment (as opposed to code in the pg directory much of which is inside Safe).  That means you can add CPAN modules easily to webwork2 code.

Importing CPAN modules into the Safe compartment is always tricky. I'm surprised that you aren't able to add Data::Dumper by adding it to the module list -- but I don't know exactly why it didn't work.  This is getting a bit specialized so you might want to ask about this on the webwork-devel mailing list to see if there is some developer who has gotten Data::Dumper to work inside Safe.

In reply to Michael Gage

Re: Which perl function from which macro file?

by Andrew Dabrowski -
Sorry, I'm still not grokking. How is it exactly that one goes about easily adding modules to webwork2 code? What file do I edit?

Thanks for the link, I'll check that out.