ObjectScript is an object programming language designed for rapidly developing complex business applications on the InterSystems IRIS Data Platform™. It is well-suited for a variety of applications including:
ObjectScript source code is compiled into object code that executes within the InterSystems IRIS Virtual Machine. This object code is highly optimized for operations typically found within business applications, including string manipulations and database access. ObjectScript programs are completely portable across all platforms supported by InterSystems IRIS.
You can use ObjectScript in any of the following contexts:
To learn more about ObjectScript, you can also refer to:
Some of the key features of ObjectScript include:
The following is an introduction to the major elements of ObjectScript.
For example, to assign a value to a variable, you can use the SET
In ObjectScript it is possible (though not recommended) to use any valid name as an identifier name, as shown in the following program, which is functionally identical to the previous example:
Whitespace can be inserted or omitted almost anywhere in ObjectScript. However, one use of whitespace is significant; a command cannot start on the first character position on a line, nor can a command continue on the first character position of a line. Thus, all command lines must be indented. Comments must also be indented. However, a label
must begin on the first character position of a line, and some other syntax, such as macro preprocessor statements, may begin on the first character position of a line. For details refer to “Whitespace
” in the “Syntax Rules” chapter of this document.
ObjectScript does not use a command terminator character or a line terminator character.
ObjectScript syntax, in its simplest form, involves invoking commands on expressions, such as:
An ObjectScript program consists of a number of statements. Each statement defines a specific action for a program to undertake. Each statement consists of a command
and its arguments
Consider the following ObjectScript statement:
is a command. It does exactly what its name implies: it writes whatever you specify as its argument(s)
to the current principal output device. In this case, WRITE
writes three arguments: the literal string “Hello”; the “!” character, which is a symbolic operator specific to the WRITE
command that issues a line feed/carriage return; and the local variable x
, which is replaced during execution by its current value. Arguments are separated by commas; you may also add whitespace between arguments (with some restrictions). Whitespace is discussed in the Syntax
chapter of this document.
Most ObjectScript commands (and many functions and special variables) have a long form and a short (abbreviated) form (typically one or two characters). For example, the following program is identical to the previous one, but uses the abbreviated command names:
The short form of a command name is simply a device for developers who do not like to type long command names. It is exactly equivalent to the long form. This document uses the long form of command names. For a complete list, see Abbreviations Used in ObjectScript
in the ObjectScript Reference
is a routine that performs an operation (for example, converting a string to its equivalent ASCII code values) and returns a value. A function is invoked within a command line. This invocation supplies parameter values to the function, which uses these parameter values to perform some operation. The function then returns a single value (the result) to the invoking command. You can use a function any place you can use an expression
A function always returns a value. Commonly, this return value is supplied to a command, such as SET namelen=$LENGTH("Fred Flintstone")
or WRITE $LENGTH("Fred Flintstone")
, or to another function, such as WRITE $LENGTH($PIECE("Flintstone^Fred","^",1))
. Failing to provide a recipient for the return value usually results in a <SYNTAX> error. However, in a few functions, providing a recipient for the return value is not required. An operation performed by executing the function (such as moving a pointer), or the setting of one of the function’s parameters is the relevant operation. In these cases, you can invoke a function without receiving its return value by using the DO
command. For example, DO $CLASSMETHOD(clname,clmethodname,singlearg)
A function can have no parameters, a single parameter, or multiple parameters. Function parameters are positional and separated by commas. Many parameters are optional. If you omit a parameter, InterSystems IRIS uses that parameter’s default. Because parameters are positional, you commonly cannot omit a parameter within a list of specified parameters. In some cases (such as $LISTTOSTRING
) you can omit a parameter within a parameter list and supply a placeholder comma. You do not have to supply placeholder commas for optional parameters to the right of the last specified parameter.
For most functions, you cannot specify multiple instances of the same parameter. The exceptions are $CASE
, and $SELECT
Commonly, a parameter can be specified as a literal, a variable, or the return value of another function. In a few cases, a parameter must be supplied as a literal. In most cases, a variable must be defined before it can be specified as a function parameter, or an <UNDEFINED> error is generated. In a few cases (such as $DATA
) the parameter variable does not have to be defined.
Commonly, function parameters are input parameters that supply a value to the function. A function does not modify the value of a variable supplied as an input parameter, In a few cases, a function both returns a value and sets an output parameter. For example, $LISTDATA
returns a boolean value indicating whether there is a list element at the specified position; it also (optionally) sets its third parameter to the value of that list element.
All functions can be specified on the right side of a SET
command (for example, SET x=$LENGTH(y)
). A few functions can also be specified on the left side of a SET
command (for example, SET $LIST(list,position,end)=x
). Functions that can be specified on the left side of a SET
are identified as such in their reference page syntax block.
System-supplied functions are provided as part of InterSystems IRIS. The ObjectScript Language Reference
describes each of the system-supplied functions. A function provided in a class is known as a method. Methods provided in InterSystems IRIS are described in the InterSystems Class Reference
Within a program, you use expressions as arguments for commands and functions:
SET x = "Hello"
WRITE 1 + 2,!
In ObjectScript, a variable is the name of a location in which a runtime value can be stored. Variables must be defined, for example, by using the SET
command, but they do not have to be typed. Variables in ObjectScript are untyped; that is, they do not have an assigned data type and can take any data value. (For compatibility, the $DOUBLE
function can be used to convert untyped floating point numbers to a specific numeric data type format.)
ObjectScript supports several kinds of variables:
Local variables A variable that is accessible only by the InterSystems IRIS process that created it, and which is automatically deleted when the process terminates. A local variable is accessible from any namespace.
Process-private globals A variable that is accessible only by the InterSystems IRIS process and is deleted when the process ends. A process-private global is accessible from any namespace. Process-private globals are especially useful for temporary storage of large data values.
Globals A persistent variable that is stored within the InterSystems IRIS database. A global is accessible from any process, and persists after the process that created it terminates. Globals are specific to individual namespaces.
Array variables A variable with one or more subscripts. All user-defined variables can be used as arrays, including local variables, process-private globals, globals, and object properties.
Special variables (also known as system variables) One of a special set of built-in variables that contain a value for a particular aspect of the InterSystems IRIS operating environment. All special variables are defined; InterSystems IRIS sets all special variables to an initial value (sometimes a null string value). Some special variables can be set by the user, others can only be set by InterSystems IRIS. Special variables are not array variables.
Object properties A value associated with, and stored within, a specific instance of an object.
ObjectScript supports various operations on or among variables. Variables are described in much greater detail in the Variables
chapter of this document.
Content Date/Time: 2019-07-23 06:35:02