admin管理员组文章数量:1122832
There are a variety of ways I could implement include guards in TCL, but this is probably the most robust way I've considered, without knowing any tricks or shortcuts:
if { ! [info exists _THIS_FILE_] } {
set _THIS_FILE_ 1
# ...
}
However, it's verbose and introduces an indent on everything.
There are a variety of ways I could implement include guards in TCL, but this is probably the most robust way I've considered, without knowing any tricks or shortcuts:
if { ! [info exists _THIS_FILE_] } {
set _THIS_FILE_ 1
# ...
}
However, it's verbose and introduces an indent on everything.
Share Improve this question asked Nov 22, 2024 at 21:56 ChrisChris 31.2k31 gold badges97 silver badges186 bronze badges2 Answers
Reset to default 3There are several possibilities. The one closest to your proposal, but without introducing indentation of all the code would be:
if {[info exists _THIS_FILE_]} return
set _THIS_FILE_ 1
# ...
The source
command will cease script evaluation when it encounters a return
command.
However, I personally prefer to write my source files in such a way that they can harmlessly be sourced multiple times. This makes it easy during development to change the code and then load the updates in an already running program.
I suppose the proper way to do what you seem to be looking for is to turn files that you may want to include from multiple places into modules. This just means that you follow some rules for naming the files and place them in a directory on the module path (tcl::tm::path list
), or adjust the module path to include the directory where your files are (tcl::tm::path add $dir/mylib
). Then you can just package require
the module in all the places where you would previously source your files. Tcl will take care of only loading each module once, so no further administration by the module developer is needed.
The source
command is far too simple-minded to do this for itself; it really is just reading the file in and evaluating the contents immediately as a script. Instead, make the file into a package (by adding package provide myPkg 1
to the file and creating a pkgIndex.tcl
to go with it). Then you get the only-once semantics when you package require
it (unless you package forget
it).
Alternatively, put this at/near the top of your script:
if {[incr ::someUniqueName] > 1} return
The best choice of unique variable name depends on all sorts of factors, but is often a good idea to pick something in your code's namespace; as long as you use the fully-qualified name or ensure the correct namespace context, it will work. (In complex cases, it might make sense to use an array of these things to group that state across multiple files, where the key is [file tail [info script]]
.)
本文标签: expectWhat is the quotnormalquot way to implement include guards in TCLStack Overflow
版权声明:本文标题:expect - What is the "normal" way to implement include guards in TCL? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300642a1930888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论