WeBWorK Main Forum

using perl modules

using perl modules

by Zak Zarychta -
Number of replies: 9
If I'd like to use a perl module such as Math::BigFloat I believe I should include it in the global.conf.dist file.

I've taken a quick look at global.conf.dist and I'm not sure of how to go about this.

Any pointers would be appreciated
Zak
In reply to Zak Zarychta

Re: using perl modules

by Zak Zarychta -
I'd also like to use the sort function to sort numerical arrays in ascending order. I have a feeling that this is connected as the error that occurs when i try to sort an array is as follows.

'sort' trapped by operation mask at line 33 of (eval 8800)  
In reply to Zak Zarychta

Re: using perl modules

by D. Brian Walton -
Regarding the sort, you should look at using num_sort that is provided for just this purpose.

http://webwork.maa.org/wiki/Num_sort

Also, if you want more complex sorting, you can use the PGsort macro.

http://webwork.maa.org/wiki/PGsort

See the following discussion:
http://webwork.maa.org/moodle/mod/forum/discuss.php?d=337&parent=1300

Best wishes,
D. Brian Walton
In reply to D. Brian Walton

Re: using perl modules

by Christopher Heckman -
(I've fixed the wiki page about PGsort, btw.)

I have a question about assigning functions to variables, so that I don't have to retype them. I want to do something like

$comparer = sub { ... }
# or
sub comparer { ... }

$L1 = PGsort ($comparer, @list1);
$L2 = PGsort ($comparer, @list2);

but I can't seem to find the exact syntax to do so. (I've tried various combinations of \ $ and % in front of the sub's name.) Is what I'm doing even possible, and if so, how?

Thanks in advance,

In reply to Christopher Heckman

Re: using perl modules

by Davide Cervone -
Your code should work as is for the variable version ($comparer). So, for example,
    $compare = sub {$_[0] < $_[1]};
    @list = (5,2,3,1,7,4,6);
    PGsort($compare,@list);  # returns (1,2,3,4,5,6,7)

If you want to use a named function, then that requires using the use of ~~& before the name when PGsort is called. (Note that PG handles \ specially; because it is used in TeX, PG doubles all occurrences of \, which makes it impossible to use it for its usual Perl meanings. So PG provides ~~ in place of \. Pretty awkward, but it works.)

So you could do

    sub compare {$_[0] < $_[1]};
    @list = (5,2,3,1,7,4,6);
    PGsort(~~&compare,@list);  # returns (1,2,3,4,5,6,7)
in this case. Note that $ and % are not what you want in this case, since $ refers to a scalar variable (like the CODE reference in the first example above), while % refers to a HASH (an associative array). You want &, which is for CODE, so \& would get you a code reference, but \ must be replaced by ~~in PG, giving ~~& here.
In reply to Davide Cervone

Re: using perl modules

by Michael Gage -
One more note. In .pg files (where TeX is used more ) the backslash is reserved
for TeX and one needs to use ~~ , as Davide describes above,  to replace \.

In .pl files however there is much more perl code than TeX code, so \
works as expected for perl and any strings which are supposed to contain
TeX backslashes must use double backslashes: e.g. "\\alpha".

This is a frequent source of confusion if you test and debug a subroutine
in a problem and then later transfer it to a macro .pl file so that it 
can be used in many webwork problems.  I still get caught by it.

-- Mike
In reply to Davide Cervone

Re: using perl modules

by Christopher Heckman -
The first isn't working for me. (Maybe I have an older version of WeBWorK?) The second does; I'd forgotten about how to handle backslashes in WeBWorK.

Thanks, and I'll be sure to pass on my new knowledge.
In reply to Christopher Heckman

Re: using perl modules

by Davide Cervone -
When you say the first "doesn't work", can you be more specific about what happens? Do you get an error message? Do you get a list that is not sorted properly? Can you post a sample file that doesn't work?

I know of no reason why the first should *not* work, and it does work in older version of WeBWorK. What version of perl are you running? (You can use perl -v on the command line on the server to tell).
In reply to Davide Cervone

Re: using perl modules

by Christopher Heckman -
It was a syntax error, and after looking closely, I figured out what the problem was. I was writing

$c = sub { ... }

instead of

$c = sub { ... };

(I.e., it was a bonehead semicolon error.) Thanks for pointing me in the right direction, though.
In reply to Christopher Heckman

Re: using perl modules

by Davide Cervone -
OK, thanks for letting us know what happened. Good to know that what should have worked does work.