TCL 相当于 Python 的 `if __name__ == "__main_ _"`

标签 tcl

在一个可执行的 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(技术上是标准 tclshwish shell 的一个特性,或在 C 级别调用 Tcl_MainTk_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/

相关文章:

linux - Expect ,需要处理表达式并在屏幕上打印结果

对只有最小值的列进行排序

linux - grep 上的 Tcl exec 返回 "grep: nothing to repeat",而正常的 LINUX 给出正确答案

namespaces - 在 Tcl 中查找命名空间的所有过程

linux - 在绘制 Tcl/Tk 图形之前调整 Y 值

arrays - 如何替换/重置 TCL 数组中的元素值?

tcl - itcl配置方法: How to use public variables with a config script?

wrapper - tcl:包装一个同名的proc

在 Windows 中为 tcl/tk starkit 创建 .dll

arrays - TCL 中的冒号和数组和命名空间