Variables TCL regsub到变量?

Variables TCL regsub到变量?,variables,tcl,regsub,Variables,Tcl,Regsub,我正在设置宏,设置,然后说。在程序中定义 proc Set {key value args} { set ::$key $value set "::key2" "$key" } proc Say {key} { puts $key } proc Say2 {key} { set key3 [regsub "\%" $key "\$"] puts $key3 eval puts $key3 } 这允许我执行以下操作: Set "test" "this shou

我正在设置宏,设置,然后说。在程序中定义

proc Set {key value args} {
    set ::$key $value
            set "::key2" "$key"
}

proc Say {key} {
puts $key
}

proc Say2 {key} {
set key3 [regsub "\%" $key "\$"]
puts $key3
eval puts $key3
}
这允许我执行以下操作:

Set "test" "this should display this test text"
Say $key2 ;#should display the key "test" which is what we just set
Say $test ;#presents the value of the key test
输出

% Set "test" "this should display this test text"
test
% Say $key2 ;#should display the key "test" which is what we just set
test
% Say $test ;#presents the value of the key test
this should display this test text
% Set "mouse" "squeak" ;#set key mouse with value string of "squeak"
mouse
% Say $mouse ;#displays the value as set above correctly
squeak
% Say2 %mouse ;#start using our own characters to represent variables
$mouse
can't read "mouse": no such variable
现在让我们假设我想重新分配变量$to%

Set "mouse" "squeak" ;#set key mouse with value string of "squeak"
Say $mouse ;#displays the value as set above correctly
Say2 %mouse ;#start using our own characters to represent variables - switch the % for a $ and then output
但是当我使用eval时

 can't read "mouse": no such variable
输出

% Set "test" "this should display this test text"
test
% Say $key2 ;#should display the key "test" which is what we just set
test
% Say $test ;#presents the value of the key test
this should display this test text
% Set "mouse" "squeak" ;#set key mouse with value string of "squeak"
mouse
% Say $mouse ;#displays the value as set above correctly
squeak
% Say2 %mouse ;#start using our own characters to represent variables
$mouse
can't read "mouse": no such variable
我觉得这很奇怪,因为我们在上面设置了它,我们可以使用标准$调用值,我可以证明Say2中的regsub工作正常,它应该用$替换%

%鼠标变为$mouse,这是一个有效变量。 Eval$鼠标输出,不带此类变量

我错过什么了吗


谢谢

问题在于
程序

proc Say2 {key} {
    set key3 [regsub {%} $key {$}]
    puts $key3
    eval puts $key3 ;# here
}
$mouse
在此
proc
中不存在。它不是作为参数传递的,也不是用
set
创建的。但是,它存在于全局命名空间中。在这种情况下,一种方法是使用
uplevel

proc Say2 {key} {
    set key3 [regsub {%} $key {$}]
    puts $key3
    uplevel puts $key3
}
我经常使用的另一个选项是
upvar
,将变量放入其中(尽管在本例中,我们不再需要
$
):


PS:我还删除了一些反斜杠,因为在这种情况下它们并不真正需要。

现在我了解了upvar的用法。太好了!