我可以使用 uplevel/upvar 而不必使用 global 吗?
所以,我正在测试一些东西,并且有一个像这样的“测试”过程:
proc test {arg} {
global state
puts "Your arg is: $arg"
set state 1
}
test somearg
vwait state
通过阅读有关 uplevel 和 upvar 的内容,有没有一种方法可以让我不必使用全局,并使用其中一个选项来设置状态变为“1”然后退出程序?
So, I am testing somethings out, and have a "test" proc like so:
proc test {arg} {
global state
puts "Your arg is: $arg"
set state 1
}
test somearg
vwait state
From reading about uplevel and upvar, is there a way that I can get away with not having to use global, and use either one of those options to set the state to "1" and then exit the program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,除了
vwait
始终使用全局变量进行等待(严格来说,它解析全局范围内的变量名称;如果提供限定名称,则可以使用其他命名空间)。您不能做的是等待局部变量(因为事件无法看到其自己的调用堆栈之外的局部变量)。也许这种情况将来会改变,但现在肯定不会。关于
global
的问题,这些语句在过程中的效果都是相同的:您的代码中还有一个错误:您在等待状态更改之前设置状态。无论如何这都是行不通的,因为你必须先等待,然后在某种事件中设置状态。
Yes, except that
vwait
always uses global variables for waiting on (strictly, it resolves variable names in the global scope; you can use other namespaces if you provide qualified names). What you can't do is wait on a local variable (because events can't see local variables outside their own call stack). Maybe this will change in the future, but certainly not now.In relation to the question about
global
, these statements are all the same in effect inside a procedure:You also have a bug in your code: you set the state before waiting for it to change. That won't work anyway because you've got to wait first, and set the state from within some kind of event.
你要求的是两件不同的事情。首先,关于变量。您可以像这样使用
upvar
:或者,更简单地,您可以只使用命名空间限定名称:
问题的后“一半”是
vwait
的一些奇怪用法。我只是想指出,您的代码片段将无法完成,因为您正在等待变量“状态”更改,但没有任何事件会更改状态。当您调用test
时,您已经更改了它。因此,除非您设置了窗口/按钮或可能导致state
状态发生更改的内容,否则您的脚本将挂起。值得阅读 wiki 和
vwait
的手册页。You're asking for two different things. First, about the variable. You can use
upvar
like so:Or, more easily, you can just use the namespace qualified name:
The second "half" of your question is some odd usage of
vwait
. I just want to point out that your snippet of code won't complete because you're waiting for the variable 'state' to change, but there's no event that will ever change state. You already changed it when you calledtest
. So, unless you've set up a window/button or something that might causestate
's state to change, your script will hang.It's worth reading the wiki and the man page for
vwait
.