Next: , Previous: Variables Overview, Up: Variables Overview



5.1.1 Scope

Global variables are available throughout your policy script (once declared), while the scope of local variables is confined to the function or event handler in which they're declared. You indicate the variable's type using a corresponding keyword:

global name : type ;
or
local name : type ;
which declares name to have the given type and the corresponding scope.

You can intermix function/event handler definitions with declarations of global variables, and, indeed, they're in fact the same thing (that is, a function or event handler definition is equivalent to defining a global variable of type function or event and associating its initial value with that of the function or event handler). So the following is fine:

         global a: count;
     
         function b(p: port): string
             {
             if ( p < 1024/tcp )
                 return "privileged";
             else
                 return "ephemeral";
             }
     
         global c: addr;

However, you cannot mix declarations of global variables with global statements; the following is not allowed:

         print "hello, world";
         global a: count;

Local variables, on the other hand, can only be declared within a function or event handler. (Unlike for global statements, these declarations can come after statements.) Their scope persists to the end of the function. For example:

         function b(p: port): string
             {
             if ( p < 1024/tcp )
                 local port_type = "privileged";
             else
                 port_type = "ephemeral";
     
             return port_type;
             }