namespace upvar

namespace upvar creates a variable a link to the specified namespace variable.


namespace upvar namespace otherVar myVar ?otherVar myVar ...?


Links a namespace variable named otherVar, which may contain namespace qualifiers and is resolved relative to namespace, into the the current level as myVar, which also may contain [namespace qualifiers.

It is an error for otherVar and myVar to resolve to the same variable unless that variable is already a name for some other variable. This makes it possible to determine whether a variable is an alias.

namespace upvar is a convenience command that doesn't add any functionality not previously available with upvar. The following are equivalent:

set ns ::foo
upvar #0 ${ns}::bar boo


set ns ::foo
namespace upvar $ns bar boo

However, the second form is more efficient as it requires less string manipulation and parsing.

In contrast to variable namespace upvar provides the ability to give the variable an arbitrary name :

proc myproc {} {
    variable var1
    namespace upvar [namespace current] var1 myvar1

DKF: namespace upvar was created to help accelerate Snit.

RFE: One and Two-argument Case

PYK 2018-05-14:

namespace upvar could support a two-argument case where the current namespace is implied. It could also support a one-argument case where the one argument is a list of alternating namespace and local names.