在TCL中使用变量
我正在尝试在TCL中的其他Proc中使用全局变量。
这是一个简短的例子:
proc myname {} {
set ::name [gets stdin]
}
proc myname2 {} {
puts "your name is: $name"
}
tcl_shell> proc myname
Jhon <--- "Jhon" should be stored in varible called *name*
tcl_shell> proc myname2
your name is Jhon <-- I want something like this.
但是我仍然看到此错误:错误:无法读取“名称”:没有这样的变量,
我也尝试过:
proc myname {} {
global name
set name [gets stdin]
}
proc myname2 {} {
puts "your name is: $name"
}
I'm trying to use a global variable in other proc in tcl.
Here's a short example:
proc myname {} {
set ::name [gets stdin]
}
proc myname2 {} {
puts "your name is: $name"
}
tcl_shell> proc myname
Jhon <--- "Jhon" should be stored in varible called *name*
tcl_shell> proc myname2
your name is Jhon <-- I want something like this.
But I'm still seeing this error: Error: can't read "name": no such variable
I also have tried this:
proc myname {} {
global name
set name [gets stdin]
}
proc myname2 {} {
puts "your name is: $name"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试读取
myname2
范围中的变量名称
:当然,
myname2 < /代码>功能。您保存的变量在全局(
::
)范围中,因此您可以执行:或
与C/C ++ TCL不同,默认情况下会隐藏全局范围。您需要故意将变量从全局范围导入到您的功能中。
You are trying to read a variable
name
that is in themyname2
scope:Of course there is no variable
name
inside themyname2
function. The variable you saved to is in global (::
) scope so you can either do:or
Unlike C/C++ tcl hides the global scope by default. You need to deliberately import variables from global scope into your functions.