TCL从名称空间外部的proc调用名称空间变量 [英] TCL calling a namespace variable from a proc outside the namespace

查看:588
本文介绍了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.

这篇关于TCL从名称空间外部的proc调用名称空间变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆