Since MIC 2.0, compliant compilers must produce code that, if source code cooperates, be strictifiable. That is, MIC will introduce no undeclared or unscoped global variables. Since global variable errors have been found to be easy to produce and sometimes hard to identify, it is highly recommended that all but a few special variables be scoped to the page. To strictify a pile, one should include a 'use strict;' statement at the top of the control file.
One should think of each of the following as a lexical block:
When, out of necessity or for the sake of efficiency, one wishes to pass variables between these lexical blocks, there are two recommended, but dependant, methods. The first is to include the 'use vars...;' statement after the 'use strict;' statement and simply have the listed variables be global. If this is done, be sure and include the special cleanup subroutine in the control file and undef each of the declared variables. This is to ensure that global state errors do not occur even when a FastCGI server is used to call the pile.3.3
There are two established cases where one should declare a global variable in this fashion. The first is to make global common objects or state indicators that will be needed by virtually every page and fieldspace.
For instance, if one were designing an interface to a MUD, then we might expect a Player object to be needed for the logic of virtually every page and fieldspace. To make said object available, we would do something like the following in the control file:
use strict; use vars qw($player); ... sub setup { #$cgi is made available by the pile driver my $player_id = get_player_id_from_cookie($cgi); $player = find Player($player_id); ... } sub cleanup { undef $player; ... } ...
The second established use for global variables is the use of a global variable used to pass information from one lexical block (see above) to another. This is often done for the sake of efficiency as it turns that a given page and its associated fieldspace (if there is one), will often have need of the same information. Also, the code within a MICdefine will very often require some of the same information that is determined in the MIC code in a different lexical block.
By convention, this special variable is called '%pf_box,' which stands for 'page/fieldspace box.' Examples of how to use %pf_box can be found in various portions of this document.