0 tcl
我想传递一个变量名称的proc并在proc中使用它.
问题是将参数传递给proc会将变量转换为其值:
set my_value [ list eee ttt yyy ]
proc my_proc { args } {
puts "MY ARGS IS :$args\n"
}
my_proc $my_value
MY ARGS IS :{eee ttt yyy}
Run Code Online (Sandbox Code Playgroud)
我想得到:
MY ARGS IS : my_value
Run Code Online (Sandbox Code Playgroud)
谢谢你
Tcl 在语义上是严格的 pass-by-value(它的实现是pass-by- immutable -reference),但是你传递的值可以是一个名字(只是不要放在$它前面,因为Tcl 总是意味着"从这个变量中读取,现在").特别是,你会这样做:
my_proc my_value
Run Code Online (Sandbox Code Playgroud)
如果你想将这个名字绑定到一个局部变量,这样你就可以读取和写入它,那么你就可以这样做(在程序中):
proc my_proc { args } {
upvar 1 [lindex $args 0] theVar
puts "MY ARGS IS :$args"
puts "THE VARIABLE CONTAINS <$theVar>"
}
Run Code Online (Sandbox Code Playgroud)