Product SiteDocumentation Site

12.3. Global Variables

Variables can be declared global. Global variables are declared in the prolog.
Example 12.4. Global variable.
declare variable $obj
  := { "foo" : "bar" };
declare variable $numbers
    := (1, 2, 3, 4, 5);
$obj,
[ $numbers ]
Results:
{
  "foo" : "bar"
}
[ 1, 2, 3, 4, 5 ]

You can specify a sequence type for a variable. If the type does not match, an error is raised. In general, you do not need to worry too much about variable types except if you want to make sure that what you bind to a variable is really what you want. In most cases, the engine will take care of types for you.
Example 12.5. Global variable with a type.
declare variable $obj as object
  := { "foo" : "bar" };
$obj
Results:
{
  "foo" : "bar"
}

An external variable allows you to pass a value from the outside environment, which can be very useful. Each implementation can choose its own way of passing a value to an external variable. A default value for an external variable can also be supplied in case none is provided from outside.
Example 12.6. An external global variable with a default value.
declare variable $obj external
  := { "foo" : "bar" };
$obj
Results:
{
  "foo" : "bar"
}

In these examples, global variables have no prefix. They can also be prefixed with the predefined alias local:, but them they must be prefixed both in the declaration and when used.
Example 12.7. An external global variable with the local: alias.
declare variable $local:obj external := { "foo" : "bar" };
$local:obj
Results:
{
  "foo" : "bar"
}

Global variables that are imported from other modules are prefixed with the alias associated with the imported module, as will be explained in Chapter 13, Modules.