一个关于 Tcl 源码的问题 [英] a question about source in Tcl
问题描述
我有一个名为 test7.tcl 的文件:
I have a file named test7.tcl:
namespace eval ::dai {
variable name "ratzip"
variable birthday "1982"
proc hello {} {
variable name
variable birthday
puts "Hello, I am $name birthday is $birthday"
}
}
并且我想以这种方式将此文件导入另一个名为 test8.tcl 的文件中:
and I want to source this file into another file, called test8.tcl in this way:
source test7.tcl
::dai::hello
但它给了我错误:无法读取文件test7.tcl":没有这样的文件或目录
but it gives me error: couldn't read file "test7.tcl": no such file or directory
但是这两个文件在同一个文件夹下,怎么回事?
but the two files are under the same folder, what happened?
推荐答案
要获取与当前正在执行的脚本位于同一目录中的文件,请使用:
To source a file that is in the same directory as the currently executing script, use this:
source [file join [file dirname [info script]] "test7.tcl"]
请注意,这在外部脚本(在您的情况下为 test8.tcl)中定义的过程中不起作用,因为它们通常在源代码完成后调用.如果您是这种情况,最简单的解决方法是将 info script
的输出保存在外部脚本的变量中(或者直接获取所有文件,而不是延迟获取最终的最佳方法).
Note that this doesn't work inside procedures defined inside the outer script (test8.tcl in your case) because they're typically called after the source finishes. If that's the case for you, the simplest fix is to just save the output of info script
in a variable in your outer script (or just source all files immediately instead of lazily for the ultimately best approach).
这篇关于一个关于 Tcl 源码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!