global, local
Full Description
swirl
Syntax
  global | local type name (variable (initial-size numeric-expression)? | size numeric-expression)?  (initial {value (with key string-expression)?)+))?

Where:
type

    An OmniMark variable type, one of stream, counter, switch, opaque.

name
    The name of the variable being declared.


Purpose

You use the keywords global and local to declare variables. Global variables are available across your entire program. Local variables are available in the local scope in which they are created.

The following code declares four global variables of different types:

  global stream my-text
  global counter my-number
  global switch my-flag
  global my-type my-object ;declare as an opaque variable of type my-type

The following code declares a local variable in the scope of a find rule:

  find letter+
     local counter my-number

You can declare initial values for your variables (but not for opaque types):

  global stream my-text initial {"Mary had a little lamb."}
  global counter my-number {6}
  global switch my-flag {true}
A stream variable may be initialized to unattached, as in the second line above, but this is the same as not initializing it at all.

You can declare fixed-size or variable-size shelves:

  global stream my-text size 3
  global counter variable

You can also declare the initial size of a variable-size shelf:

  local switch my-flags variable initial-size 0
You should make it a habit to always declare the initial size of your variable shelves as this can help avoid errors. (OmniMark makes no actual distinction between single-value variables and shelves. A single-value variable is a shelf with one item, therefore the default number of items on a shelf is one. But in most cases you will want to declare your variable shelves with an initial size of zero and add items in your code. If you fail to declare the initial size, your shelf will have a spurious, and unkeyed, item that may cause errors if your code attempts to repeat over the shelf.)

You can declare initial values for both fixed-size and variable-size shelves. For variable-size shelves, the number of initial values must match the number of items on the shelf. For fixed-sized shelves, the number of initial values cannot exceed the size of the shelf.

  local stream animals size 4 initial {"cow", "dog", "pig", "hen"}
  local switch my-flags variable initial-size 3 initial {true, false, true}

You can also declare the initial key values for the values you declare:

  local switch my-flags variable initial-size 3
     initial {true with key "mary", false with key "lamb", true with key "school"}

Finally, you can omit the initial size of a variable shelf when declaring initial values. The initial size of the shelf will be set to the number of initial values specified:

  local switch my-flags variable
     initial {true with key "mary", false with key "lamb", true with key "school"}

You can only declare a local variable once in a scope. You can declare a global variable more than once providing the declarations are the same. This allows you to declare a variable in an include file and in the main program file without error.

Variables that you intend to initialize on the command line (using the -os, -define, -activate, or -counter command-line options) must be global and must not be on a fixed-size shelf, unless its size is 1. Also, variable-size shelves that have an initial size greater than 1 may not be initialized on the command line.

Copyright © OmniMark Technologies Corporation, 1988-1998.