swirl Guide to OmniMark 5   OmniMark home
docs home 
IndexConceptsTasksSyntaxLibrariesOMX VariablesErrors
 
    Related Concepts  
declaration/definition   global, local    

Syntax

  global type name (variable (initial-size numeric-expression)? | size numeric-expression)?  (initial {expression (with key string-expression)?)+))?

  local type name (variable (initial-size numeric-expression)? | size numeric-expression)?  (initial {expression (with key string-expression)?)+))?

Where:
type


name

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 integer my-number
  global switch my-flag
  global TCPConnection connection ;declare as an OMX variable of type TCPConnection

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

  find letter+
     local integer my-number

You can declare initial values for your variables:

  global stream my-text initial {"Mary had a little lamb."}
  global integer my-number initial {6}
  global switch my-flag initial {true}

You can initialize variables using dynamic expressions:

  global bcd foo initial {6.5}
  global bcd bar initial {foo + 9.3}
  global integer port-number initial {5300 + #args[3]}
  global TCPService service {tcpServiceOpen at port-number}
  local TCPConnection connection initial {TCPServiceAcceptConnection service}

Any value you use in a dynamic initializer must itself be initialized before it is used.

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

  global stream my-text size 3
  global integer variable

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

  local switch my-flags variable initial-size 0

Note that the initial size of a shelf declared variable is 0 in OmniMark 5 and all subsequent versions. It was 1 in previous versions of the language. If you have a program written for a previous version that assumes, rather than declaring, that the initial size of a variable sized shelf is 1, you should make this explicit by adding initial-size 1 to the declaration. The code will then work identically on all versions.

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-size 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 -integer 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.

The default values of OmniMark's built-in variable types are as follows:

      Related Concepts
   Counter data type
   Integer data type
   Shelves
   Switch data type
   Variables
 
----

Top [ INDEX ] [ CONCEPTS ] [ TASKS ] [ SYNTAX ] [ LIBRARIES ] [ OMX ] [ OMX ] [ ERRORS ]

Generated: August 11, 2000 at 3:07:18 pm
If you have any comments about this section of the documentation, send email to docerrors@omnimark.com

Copyright © OmniMark Technologies Corporation, 1988-2000.