Reputation: 3235
namespace must be plain text, it cannot be meta elements such as variable or nesting script. E.g. there is no way you can do the following:
set ns ::my_ns
set ::my_ns::var1 100
puts ${$ns}var1 <== wrong
puts $$nsvar1 <== wrong
Could you confirm?
Upvotes: 0
Views: 64
Reputation: 137557
You can't use the $
syntax to do this; this is a syntactic limitation. You have to use something like the single-argument form of set
:
puts [set ${ns}::var1]
BUT you have a much better option available to you if you are inside a procedure. The namespace upvar
command handles this sort of thing nicely:
proc printVariable {ns} {
namespace upvar $ns var1 v
puts $v
}
Also, if you're doing this to simulate an object system, please stop. Use a real object system instead (e.g., TclOO, which ships as a built-in part of Tcl 8.6).
Upvotes: 1
Reputation: 4372
This works:
% namespace eval my_ns { set var1 100 }
100
% incr ::my_ns::var1
101
% set ns ::my_ns
::my_ns
% puts [set ${ns}::var1]
101
Upvotes: 2