获取 TCL 中从另一个脚本调用的过程的路径

发布于 2024-10-20 04:26:24 字数 637 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2024-10-27 04:26:24

info script 的结果取决于当前最里面的,并且过程不维护该信息。 (嗯,它在 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"
}

请注意,我们使用规范化的文件名,这样即使您使用相对路径名然后 cd 到其他目录,它仍然有效。
(我还建议将 test2.tcl 的全部内容放入其自己的命名空间中;这样可以更轻松地将内容分开。)

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"
}

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文