Version 12 of namespace upvar

Updated 2016-02-25 05:29:40 by pooryorick

namespace upvar creates an alias to a namespace variable.

Synopsis

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

Description

namespace upvar links otherVar to myVar at the current level, after which an operation on otherVar is identical to the same operation on myVar, and vice versa. otherVar is resolved relative to namespace, and may contain namespace qualifiers. myVar is resolved relative to the current level, and may also contain namespace qualifiers.

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

and

set ns ::foo
namespace upvar $ns bar boo

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

namespace upvar can be used in as a form of variable that provides the ability name the variable something else at the current level:

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

DKF: namespace upvar was created to help accelerate Snit.

See Also

namespace
the primary command for working with namespaces