Tcl Built-In Commands


NAME

variable - create and initialize a namespace variable

SYNOPSIS

variable ?name value...? name ?value?

DESCRIPTION

This command is normally used within a namespace eval command to create one or more variables within a namespace. Each variable name is initialized with value. The value for the last variable is optional.

If a variable name does not exist, it is created and given the optional value. If it already exists, it is simply set to the optional value. Normally, name is unqualified (does not include the names of any containing namespaces), and the variable is created in the current namespace. If name includes any namespace qualifiers, the variable is created in the specified namespace.

If the variable command is executed inside a Tcl procedure, it creates local variables linked to the corresponding namespace variables. In this way the variable command resembles the global command, although the global command only links to variables in the global namespace. If any values are given, they are used to modify the values of the associated namespace variables. If a namespace variable does not exist, it is created and optionally initialized.

A name argument cannot reference an element within an array. Instead, name should reference the entire array, and the initialization value should be left off. After the variable has been declared, elements within the array can be set using ordinary set or array commands.

It is generally best to provide a value to initialize each variable, or to initialize it immediately after the variable command. This is because a namespace variable declared by a variable command is not actually created until it is given a value. A declared but not yet initialized namespace variable will not appear in the output of an info vars command, for example.

SEE ALSO

global(n), namespace(n)

KEYWORDS

global, namespace, procedure, variable

Last change: 8.0

[ tcl8.0b1 | tk8.0b1 | X-ref ]

Copyright © 1989-1994 The Regents of the University of California.
Copyright © 1994-1997 Sun Microsystems, Inc.