获取从另一个脚本调用的 TCL 中的 proc 路径 [英] Getting path of the proc in TCL which is invoked from another script
问题描述
我是 TCL 编程的新手
i am a newbie in TCL Programming
我在两个不同的地方分别有一个名为 test1.tcl 和 test2.tcl 的 tcl 脚本
I am having a tcl script called test1.tcl and test2.tcl separately in two different
目录 F:\TCLPrograms\SamplePrograms\test1.tcl 和 F:\TCLPrograms\test2.tcl
directories F:\TCLPrograms\SamplePrograms\test1.tcl and F:\TCLPrograms\test2.tcl
我想知道作为proc的test2.tcl的完整路径
i want to know the full path of test2.tcl which is a proc
如果我在 proc disp {} 中提供信息 [script],它会返回调用它的路径
if i give info [script] inside proc disp {} its returning the path from where it is invoked
即 F:\TCLPrograms\SamplePrograms\test1.tcl
i.e F:\TCLPrograms\SamplePrograms\test1.tcl
请告诉我获取proc的路径
kindly someone tell me to get the path of the proc
test1.tcl:
puts "Processing test1..."
source "F:\\TCLPrograms\\test2.tcl"
set rc [disp]
puts "Executed...."
test2.tcl:
proc disp { } {
puts "Successfully executed test2.tcl"
set path [info script]
puts "Script is invoked from the path: $path"
}
提前致谢
推荐答案
info script
的结果取决于current最里面的source
,和程序不维护该信息.(嗯,它在 8.6 的调试信息和 ActiveState 的一些 8.5 版本中维护,但访问起来确实很尴尬.)
The result of info script
depends on the current innermost source
, and procedures don't maintain that information. (Well, it's maintained in debugging information for 8.6 and some builds of 8.5 from ActiveState, but it's truly awkward to access.)
最简单的方法是使用一个变量来保存文件名,就像这样:
The easiest way is to use a variable to hold the name of the file, like this:
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"
}
请注意,我们使用规范化的文件名,因此即使您使用相对路径名然后 cd
到其他目录,它仍然有效.(我还建议将 test2.tcl
的全部内容放在自己的命名空间中;这样可以更容易地将内容分开.)
Note that we use the normalized filename, so that it remains valid even if you use a relative pathname and then cd
to some other directory.
(I also recommend putting the whole contents of test2.tcl
inside its own namespace; it makes it easier to keep things separate.)
这篇关于获取从另一个脚本调用的 TCL 中的 proc 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!