Namespaces can be nested inside other namespaces. Example 14-3 shows three namespaces that have their own specific variable x. The fully qualified names for these variables are ::foo::x, ::bar::x, and ::bar::foo::x. Example 14-3 Nested namespaces. namespace eval foo { variable x 1 ;# ::foo::x } namespace eval bar { variable x 2 ;# ::bar::x namespace foo { variable x 3 ;# ::bar::foo::x } puts $foo::x ;# prints 3 } puts $foo::x ;# prints 1 | Partially qualified names can refer to two different objects. |
In Example 14-3 the partially qualified name foo::x can reference one of two variables depending on the current namespace. From the global scope the name foo::x refers to the namespace variable x inside ::foo. From the ::bar namespace, foo::x refers to the variable x inside ::bar::foo. If you want to unambiguously name a variable in the current namespace, you have two choices. The simplest is to bring the variable into scope with the variable command: variable x set x something If you need to give out the name of the variable, then you have two choices. The most general solution is to use the namespace current command to create a fully qualified name: trace variable [namespace current]::x r \ [namespace current]::traceproc However, it is simpler to just explicitly write out the namespace as in: trace variable ::myname::x r ::myname::traceproc The drawback of this approach is that it litters your code with references to ::myname::, which might be subject to change during program development. |