获取 TCL 中从另一个脚本调用的过程的路径
我是 TCL 编程的新手
我有一个名为 test1.tcl 和 test2.tcl 的 tcl 脚本,分别位于两个不同的
目录 F:\TCLPrograms\SamplePrograms\test1.tcl 和 F:\TCLPrograms\test2.tcl
我想知道test2.tcl 的完整路径,
如果我在 proc disp {} 内提供 info [script] ,它会返回调用它的路径,
即 F:\TCLPrograms\SamplePrograms\test1.tcl
请有人告诉我获取路径过程
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"
}
提前致谢
i am a newbie in TCL Programming
I am having a tcl script called test1.tcl and test2.tcl separately in two different
directories F:\TCLPrograms\SamplePrograms\test1.tcl and F:\TCLPrograms\test2.tcl
i want to know the full path of test2.tcl which is a proc
if i give info [script] inside proc disp {} its returning the path from where it is invoked
i.e F:\TCLPrograms\SamplePrograms\test1.tcl
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"
}
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
info script
的结果取决于当前最里面的源
,并且过程不维护该信息。 (嗯,它在 8.6 的调试信息和 ActiveState 的一些 8.5 版本中维护,但访问起来确实很尴尬。)最简单的方法是使用变量来保存文件名,如下所示:
请注意,我们使用规范化的文件名,这样即使您使用相对路径名然后
cd
到其他目录,它仍然有效。(我还建议将
test2.tcl
的全部内容放入其自己的命名空间中;这样可以更轻松地将内容分开。)The result of
info script
depends on the current innermostsource
, 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:
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.)