在一个可执行的 TCL 脚本中,我定义了一个变量,我想在另一个可执行的 TCL 脚本中导入该变量。在 Python 中,可以通过在脚本底部使用以下习语来制作组合库和可执行文件:
# Library
if __name__ == "__main__":
# Executable that depends on library
pass
TCL有什么等价的吗? There is for Perl .
最佳答案
Tcl 的等效项是比较 ::argv0
info script
结果的全局变量命令。
if {$::argv0 eq [info script]} {
# Do the things for if the script is run as a program...
}
::argv0
global(技术上是标准 tclsh
和 wish
shell 的一个特性,或在 C 级别调用 Tcl_Main
或 Tk_Main
的任何其他东西)具有主脚本的名称,或者是空字符串(如果有)没有主脚本。 info script
命令返回当前正在评估的文件的名称,是否由 source
或者因为主 shell 将它作为脚本运行。当当前脚本是主脚本时,它们将是相同的。正如 mrcalvin 在下面的评论中指出的那样,如果您的库脚本有时用于
argv0
的上下文中未设置(自定义 shell、子解释器、嵌入式解释器、某些应用程序服务器等),那么您应该先添加更多检查:if {[info exists ::argv0] && $::argv0 eq [info script]} {
# Do the things for if the script is run as a program...
}
关于TCL 相当于 Python 的 `if __name__ == "__main_ _"`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49873768/