对于特定过程运行不是本地的Tcl变量需要绑定到命名空间;命名空间可以是全局命名空间(有一个特殊的命令),但不需要。因此,有是两个程序之间共享的变量,你需要给它暴露名:现在
proc testList {{setup_file ""}} {
# Use the 'eq' operator; more efficient for string equality
if {$setup_file eq ""} {
set setup_file location
}
global setupFile
set setupFile $setup_file
}
proc run {} {
global setupFile
puts "$setupFile"
}
,这就是分享一个完整的变量。如果您只想分享价值,还有其他一些选择。例如,这两种可能性:
proc testList {{setup_file ""}} {
if {$setup_file eq ""} {
set setup_file location
}
# Create a procedure body at run-time
proc run {} [concat [list set setupFile $setup_file] \; {
puts "$setupFile"
}]
}
proc testList {{setup_file ""}} {
if {$setup_file eq ""} {
set setup_file location
}
# Set the value through combined use of aliases and a lambda term
interp alias {} run {} apply {setupFile {
puts "$setupFile"
}} $setup_file
}
没有与Tcl的8.6更多的选择,但仍处于测试阶段。