关于Tcl源码的问题
我有一个名为 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 的文件中:
source test7.tcl
::dai::hello
但它给了我错误:无法读取文件“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"
}
}
and I want to source this file into another file, called test8.tcl in this way:
source test7.tcl
::dai::hello
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要获取与当前正在执行的脚本位于同一目录中的文件,请使用以下命令:
请注意,此不能在外部脚本(在您的情况下是 test8.tcl )内定义的过程中工作,因为它们通常在源完成后调用。如果您遇到这种情况,最简单的解决方法是将
info script
的输出保存在外部脚本的变量中(或者立即获取所有文件,而不是延迟获取最终的最佳方法)。To source a file that is in the same directory as the currently executing script, use this:
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).使用
source [file join [file dirname [info script]] test7.tcl]
- 这样您就可以通过从执行文件的完整路径名构造的完整路径名来获取目标文件代码>源;无论执行期间当前目录是什么,这都将起作用。Use
source [file join [file dirname [info script]] test7.tcl]
-- that way you'll be sourcing the target file by its full pathname constructed from the full pathname of the file executingsource
; this will work no matter what your current directory is during the execution.您不必指定要获取的文件的路径相对于
test8.tcl
的路径,而是相对于当前工作目录。例如使用绝对路径:You don't have to specify the path of the file to be sourced relative to the path of
test8.tcl
but relative to the current working directory. E.g. use the absolute path: