## WeBWorK Problems

### Re: global variables

by Michael Gage -
Number of replies: 0
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