Product SiteDocumentation Site

6.2. Global variables

VarDecl
Figure 6.9. VarDecl

Variables can be declared global. Global variables are declared in the prolog.
Example 6.1. Global variable
  declare variable $obj := { "foo" : "bar" };
  $obj
Result (run with Zorba):
{ "foo" : "bar" }

Example 6.2. Global variable
  declare variable $numbers := (1, 2, 3, 4, 5);
  [ $numbers ]
Result (run with Zorba):
[ 1, 2, 3, 4, 5 ]

You can specify a type for a variable. If the type does not match, an error is raised. Types will be explained later. 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 6.3. Global variable with a type
  declare variable $obj as object := { "foo" : "bar" };
  $obj
Result (run with Zorba):
{ "foo" : "bar" }

An external variable allows you to pass a value from the outside environment, which can be very useful. Each implementation can choose their 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 outside.
Example 6.4. An external global variable
  declare variable $obj external;
  $obj
Result (run with Zorba):
An error was raised: "obj": variable has no value

Example 6.5. An external global variable with a default value
  declare variable $obj external := { "foo" : "bar" };
  $obj
Result (run with Zorba):
{ "foo" : "bar" }