在一个可执行的TCL脚本中,我正在定义一个我想在另一个可执行的TCL脚本中导入的变量.在Python中,可以通过在一个脚本底部使用以下习语来组合库和可执行文件:
# Library
if __name__ == "__main__":
# Executable that depends on library
pass
Run Code Online (Sandbox Code Playgroud)
TCL有同等的东西吗?有Perl.
Tcl的等价物是将::argv0
全局变量与info script
命令的结果进行比较.
if {$::argv0 eq [info script]} {
# Do the things for if the script is run as a program...
}
Run Code Online (Sandbox Code Playgroud)
在::argv0
全球(标准的技术上的特征tclsh
和wish
炮弹,或其他任何要求Tcl_Main
或Tk_Main
在C级)的主脚本的名称,或为空字符串,如果没有主脚本.该info script
命令返回当前正在评估的文件的名称,无论是source
由于主shell还是由于主shell将其作为脚本运行.当前脚本是主脚本时,它们将是相同的.
正如mrcalvin在下面的评论中指出的,如果您的库脚本有时用于argv0
未设置的上下文(自定义shell,子解释器,嵌入式解释器,某些应用程序服务器等),那么您应首先添加一些检查:
if {[info exists ::argv0] && $::argv0 eq [info script]} {
# Do the things for if the script is run as a program...
}
Run Code Online (Sandbox Code Playgroud)