在TCL中获取从另一个脚本调用的proc的路径

Viv*_*vek 3 tcl

我是TCL编程的新手

我有两个不同的tcl脚本分别名为test1.tcl和test2.tcl

目录F:\ TCLPrograms\SamplePrograms\test1.tcl和F:\ TCLPrograms\test2.tcl

我想知道test2.tcl的完整路径,这是一个proc

如果我在proc disp {}中给出info [script],它返回调用它的路径

即F:\ TCLPrograms\SamplePrograms\test1.tcl

请有人告诉我要获得过程的路径

test1.tcl:

puts "Processing test1..."
source "F:\\TCLPrograms\\test2.tcl"
set rc [disp]
puts "Executed...."
Run Code Online (Sandbox Code Playgroud)

test2.tcl:

proc disp { } {
puts "Successfully executed test2.tcl"
set path [info script]
puts "Script is invoked from the path: $path"
}
Run Code Online (Sandbox Code Playgroud)

提前致谢

Don*_*ows 6

结果info script取决于当前最内层source,而程序不保留该信息.(好吧,它保存在8.6的调试信息和来自ActiveState的8.5版本中,但访问真的很难.)

最简单的方法是使用变量来保存文件的名称,如下所示:

variable dispScriptFile [file normalize [info script]]
proc disp {} {
    variable dispScriptFile
    puts "Successfully executed test2.tcl"
    set path [file dirname $dispScriptFile]
    puts "Script is invoked from the path: $path"
}
Run Code Online (Sandbox Code Playgroud)

请注意,我们使用规范化的文件名,因此即使您使用相对路径名然后cd使用其他目录,它仍然有效.(我还建议将整个内容test2.tcl放在自己的命名空间中;这样可以更容易地将事物分开.)