## WeBWorK Problems

### global variables

by Zak Zarychta -
Number of replies: 1
When recently authoring a problem that involved some amount of pulling values from arrays I wrote and tested values in perl before coding the questions up in WW.
many of the variables I have are tied up in a while loop.

How do I make these global so that they can be seen outside of the loop in WeBWorK?

Zak

### Re: global variables

by Michael Gage -
my $foobar = 45; creates a local variable;$foobar2 = 45;

creates a global variable. It's full name is \$main::foobar2 if it was created in a PG problem. You seldom need to use the full name.

Only global variables can be seen inside BEGIN_TEXT/END_TEXT BEGIN_PGML/END_PGML BLOCKS.

Usually global variables are used in a PG problem. It's simpler, global variables can be interpolated inside the text blocks, and the PG problems are usually short and are always run in isolation from everything else (and from each other).

If you write a subroutine inside the PG problem, or in a macro .pl file then you should use local variables ("my" variables) whenever possible.

Here are some other references on basic perl and how PG differs from it
only slightly.

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

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