The variable
command ensures that a given namespace variable is created. Until a value is assigned to it, the variable's value is undefined:
namespace eval mynamespace {
variable alpha
set alpha 0
}
The variable can be accessed from outside the namespace (from anywhere, in fact) by attaching the name of the namespace to it:
set ::mynamespace::alpha
Access can be simplified within a procedure by using the variable
command again:
proc ::mynamespace::myproc {} {
variable alpha
set alpha
}
This creates a local alias for the namespace variable.
For a procedure defined in another namespace, the variable name must contain the namespace in the invocation of variable
:
proc myproc {} {
variable ::mynamespace::alpha
set alpha
}