Table of Contents
Tcl evaluates variables within a scope
global scope.
The scope in which a variable will be evaluated can be changed
with the global and upvar commands.
The global command will cause a
variable in a local scope (inside a procedure) to refer to the
global variable of that name.
The upvar command is similar. It
"ties" the name of a variable in the current scope to a variable in
a different scope. This is commonly used to simulate
pass-by-reference to procs.
You might also encounter the variable command in others' Tcl code. It is part
of the namespace system and is discussed in detail in that
chapter.
Normally, Tcl uses a type of "garbage collection" called
reference counting in order to automatically clean up variables
when they are not used anymore, such as when they go "out of scope"
at the end of a procedure, so that you don't have to keep track of
them yourself. It is also possible to explicitly unset them with
the aptly named unset command.
The syntax for upvar is:
upvar
?level?
otherVar1
myVar1
?otherVar2
myVar2?
...
?otherVarN
myVarN?
The upvar command causes
myVar1 to become a reference to
otherVar1, and myVar2 to become a reference to otherVar2, etc. The otherVar
variable is declared to be at level
relative to the current procedure. By default level is 1, the next level up.
If a number is used for the level, then
level references that many levels up the stack from the current
level.
If the level number is preceded by
a # symbol, then it references that
many levels down from the global scope. If level is #0, then the
reference is to a variable at the global level.
If you are using upvar with anything except #0 or 1, you are most likely asking for trouble, unless you really know what you're doing.
You should avoid using global variables if possible. If you have a lot of globals, you should reconsider the design of your program.
Note that since there is only one global space it is surprisingly easy to have name conflicts if you are importing other peoples code and aren't careful. It is recommended that you start global variables with an identifiable prefix to help avoid unexpected conflicts.