wiki:Doc/panc/dml/variables

Variables

To ease data handling, you can use variables in any DML expression. They are by default lexically scoped to the outermost enclosing DML expression. They do not need to be declared before they are used. The local variables are destroyed once the outermost enclosing DML block terminates.

As a first approximation, variables work the way you expect them to work. They can contain properties and resources and you can easily access resource children using square brackets:

# populate /table which is a nlist 
’/table/red’ = ’rouge’; 
’/table/green’ = ’vert’; 

’/test’ = { 
  x = list(’a’, ’b’, ’c’); # x is a list 
  y = value(’/table’);     # y is a nlist 
  z = x[1] + y[’red’];     # z is a string 
  return(length(z));       # this will be 6 
}; 

Local variables are subject to primitive type checking. So the primitive type of a local variable cannot be changed unless the variable is assigned a value of 'undef' or 'null' between the type-changing assignments.

Global variables (defined with the 'variable' statement) can be read from the DML block. Global variables may not be modified from within the block; attempting to do so will abort the execution.

Global and local variables share the same namespace. Consequently, there may be unintended naming conflicts between them. The best practice to avoid this, is to name all local variables with all lowercase letters and all global variables with all uppercase letters.

Last modified 17 years ago Last modified on Mar 7, 2007, 3:59:58 PM