Skip to main content

Local Variables

Local Variables

A local variable is a variable that is available in memory within the current InterSystems IRIS process. It is accessible only to the process that created it. It is accessible from all namespaces; that is, if the process changes namespaces, the variable is still available. When a process ends, all of its local variables are deleted.

Naming Conventions

Rules and Guidelines for Identifiers provides all the details, but briefly:

  • For a local variable name, the first character must be either a letter or the percent (%) character.

    Variable names starting with the % character are known as “percent variables” and have different scoping rules. In your code, for a local percent variable, start the name with %Z or %z; other names are reserved for system use.

  • All variable names are case-sensitive, and this includes local variable names. For example: MYVAR, MyVar, and myvar are three different local variables.

  • Local variable names must be unique for the current process. Other processes may have local variables with the same name.

  • A process-private global or a global may have the same (apparent) name as a local variable. For example: myvar, ^||myvar, and ^myvar are three different variables.

  • Local variables can take subscripts. See Rules About Subscripts.

A local variable name that does not follow the rules generates a <SYNTAX> error. There is one exception: if an attempted variable name begins with an underscore character followed by a letter, the system generates a <_CALLBACK SYNTAX> error. For example, SET x=_a.

Scope of Local Variables

In ObjectScript code, all local variables are public, and can thus be accessed by any operation executed by that process in the current context. Access to a local variable value is restricted as follows:

  • The NEW command creates a new local variable context. An argumentless NEW creates a new context in which none on the existing local variables are defined. NEW var creates a new context in which the local variable var is not defined. The QUIT command reverts to the prior local variable context.

  • Within a procedure block local variables are private by default. A private local variable is only defined within that procedure block.

    Local variables within a procedure block behave as follows:

    • Private variables. A local variable used within a procedure block is a private variable and is only defined within that procedure block, unless it is declared a public variable or it is a % variable. By default, all object methods created with Studio use procedure blocks (the ProcedureBlock class keyword is set within the class definition) and so, by default, all variables created in methods are private variables. You cannot use the NEW command on a private variable in a procedure block.

    • Public variables. A procedure block can explicitly declare a list of local variables as public variables. These variables values are accessible outside the procedure block. This comma-separated list of public variables can include non-existent variables and % variables. You can use the NEW command on a public variable in a procedure block.

      A public variables list for the two local variables var1 and var2 is specified as follows: MyProc(x,y) [var1,var2] PUBLIC { code body }. (Note that the PUBLIC keyword specifies that the procedure is public; it has nothing to do with the public variables list.) Public variables are specified as a comma-separated list. Only unsubscripted local variables can be specified; specifying an unsubscripted variable in the public variables lists makes all of its subscript levels public as well. Only a simple object reference (OREF) can be specified; specifying an OREF in the public variables lists makes all of its object properties public as well. The list of public variables can include undefined variables.

    • % Variables. A local variable whose name starts with “%” is automatically declared a public variable. This makes it possible to define variables that are visible to all code within a process without having to explicitly list these variables as public. Only variables that begin with “%Z” or “%z” are available for application code; all other % variables are reserved for system use according to the rules described in Rules and Guidelines for Identifiers. You can use the NEW command on a % variable in a procedure block.

    These mechanisms are described in greater detail in Procedure Variables.

  • The XECUTE command defines local variables as public by default. You can explicitly define a local variable as private within the XECUTE command. Refer to XECUTE for more on explicitly defining local variables as either private or public.

You can use the WRITE or ZWRITE command, with no arguments, to list all currently defined local variables. You can use the KILL command to delete local variables.

FeedbackOpens in a new tab