TCL从名称空间外部的proc调用名称空间变量

问题描述:

我几乎是个新手,我试图在使用argv的命名空间中设置一些变量,然后从命名空间之外的proc调用它们,但是我在理解如何做时遇到了麻烦这.我正在尝试使用这样的代码(但显然这是错误的方法):

I'm pretty much a newb, and I'm trying to set some variables in a namespace which use argv, and then to call them from a proc outside of the namespace, but I'm having trouble understanding how to do this. I'm trying to use some code like this (but clearly this is the wrong way to do it):

namespace eval Ns {
    variable spec [lindex $argv 1]
}

proc p {} {
    set spec "::Ns::spec"

}

正确的方法是使用variable:

proc p {} {
    variable ::Ns::spec
    # ...
}

也可能是upvar:

proc p {} {
    upvar #0 ::Ns::spec spec
    # ...
}

或(几乎)像您一样:

proc p {} {
   set spec $::Ns::spec
   # ...
}

如果在proc中对其进行了更改,则最后一种可能性不会更改该变量.

This last possibility will not change the variable if it is changed in the proc.