我有一个“库”类型的文件,可以在其中定义名称空间并导出一些函数以在TCL中“打印”调试信息或“跟踪”信息。
proc cputs { args } {
puts "Test= $args"
}
我将此功能导入其他多个文件中。当我使用它时,除了将括号添加到输出之外,其他一切都很好。
就像我打电话给cputs "Hi there"
一样,它将输出{Hi there}
而不是Hi there
。
找不到任何可以解释的内容。我想搭这些支架。
任何帮助将不胜感激。
谢谢
答案 0 :(得分:2)
如果您不想将任意数量的未知数量的参数传递给cproc
,请不要使用args
(作为参数名称具有特殊含义),而请使用{{1} }。
msg
另请参阅proc
documentation:
如果最后一个形式参数的名称为“ args”,则调用 过程可能包含比过程更多的实际参数 形式论据。在这种情况下,所有实际参数开始 在将要分配给args的位置合并到一个列表中(如 如果已使用list命令);该组合值分配给 局部变量args。
答案 1 :(得分:1)
如果要接受任意数量的参数(由于args
是一个特殊名称),则在打印之前应该先进行join
或concat
的操作。这两个选项对多余空格的处理方式有所不同。使用适合您的那个。
proc cputs { args } {
puts "Test= [join $args]"
}
cputs "Hello there " " from an example"
# Test= Hello there from an example
proc cputs { args } {
puts "Test= [concat {*}$args]"
}
cputs "Hello there " " from an example"
# Test= Hello there from an example