当值有多个单词时,tcl Uplevel set命令失败

use*_*011 0 tcl uplevel

全部,我想做的事:Proc A调用Proc B,使用B的uplevel命令我试图在proc A范围中设置变量.值具有空格时发生错误.

proc B { } {
    set string1 "Test"
    set string2 "Test with space"
    uplevel 1 set key1 $string1
    uplevel 1 set key2 $string2
    return 0
}

proc A { } {
    set res [B]
    puts "key1 is $key1"
    puts "key2 is $key2"
}
Run Code Online (Sandbox Code Playgroud)

如果我注释掉key2,它工作正常.添加key2时,它会失败,并显示以下错误.

wrong # args: should be "set varName ?newValue?"
    while executing
"set key2 Test with space"
Run Code Online (Sandbox Code Playgroud)

有关如何克服此错误的任何建议.感谢您的帮助.

Bry*_*ley 5

uplevel 1 set key2 $string2成为set key1 test with space,这就是你得到错误的原因.

最佳做法是使用[list]以下命令构建要运行的命令:

uplevel 1 [list set key2 $string2]
Run Code Online (Sandbox Code Playgroud)

这将保证良好的set声明.