我正在设置宏、Set 和 Say。在程序中定义。
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"
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 "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 变成 $mouse 是一个有效的变量。没有此类变量的 Eval $mouse 输出
我错过了什么吗?
谢谢