我可以使用 uplevel/upvar 而不必使用 global 吗?

发布于 2024-11-02 15:21:33 字数 249 浏览 5 评论 0原文

所以,我正在测试一些东西,并且有一个像这样的“测试”过程:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2024-11-09 15:21:33

是的,除了 vwait 始终使用全局变量进行等待(严格来说,它解析全局范围内的变量名称;如果提供限定名称,则可以使用其他命名空间)。您不能做的是等待局部变量(因为事件无法看到其自己的调用堆栈之外的局部变量)。也许这种情况将来会改变,但现在肯定不会。

关于global的问题,这些语句在过程中的效果都是相同的:

global foo
variable ::foo
upvar #0 foo foo
upvar ::foo foo
namespace upvar :: foo foo

您的代码中还有一个错误:您在等待状态更改之前设置状态。无论如何这都是行不通的,因为你必须先等待,然后在某种事件中设置状态。

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:

global foo
variable ::foo
upvar #0 foo foo
upvar ::foo foo
namespace upvar :: foo foo

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.

執念 2024-11-09 15:21:33

你要求的是两件不同的事情。首先,关于变量。您可以像这样使用 upvar

proc test {arg} {
    upvar #0 state state
    puts "Your arg is: $arg"
    set state 1
}

或者,更简单地,您可以只使用命名空间限定名称:

proc test {arg} {
  puts "Your arg is: $arg"
  set ::state 1
}

问题的后“一半”是 vwait 的一些奇怪用法。我只是想指出,您的代码片段将无法完成,因为您正在等待变量“状态”更改,但没有任何事件会更改状态。当您调用 test 时,您已经更改了它。因此,除非您设置了窗口/按钮或可能导致 state 状态发生更改的内容,否则您的脚本将挂起。

值得阅读 wikivwait 的手册页

You're asking for two different things. First, about the variable. You can use upvar like so:

proc test {arg} {
    upvar #0 state state
    puts "Your arg is: $arg"
    set state 1
}

Or, more easily, you can just use the namespace qualified name:

proc test {arg} {
  puts "Your arg is: $arg"
  set ::state 1
}

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 called test. So, unless you've set up a window/button or something that might cause state's state to change, your script will hang.

It's worth reading the wiki and the man page for vwait.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文