在TCL上的另一个进程中使用进程

  • 本文关键字:进程 另一个 TCL tcl vmd
  • 更新时间 :
  • 英文 :


我正在尝试用tcl编写一个脚本来对VMD执行一些分析。首先,我在一个过程中创建一个原子选择,然后尝试在另一个过程上使用它。

来自VMD的原子选择被实现为Tcl函数。从atomselect返回的数据是要使用的函数的名称。你可以在这里阅读更多。

当我在proc外部创建原子选择时,我可以在global内部使用它。但现在我在一个内部创建它,当我尝试使用它时,它只是一个带有过程名称的字符串。

继续

这项工作:

set all [atomselect top all]
proc test {} {
global all
$all num
}
test # Outuputs 7111

但这不是:

proc create {} {
global all
set all [atomselect top all]
}
proc test {} {
global all
$all num
}
create
test # Outputs -> invalid command name "atomselect1"

问题是,当过程返回时,atomselect命令创建的命令将被删除。我强烈怀疑这是使用局部变量删除跟踪(在某些未使用的变量上(完成的。当atomselect在全局范围内运行时,没有这样的自然删除事件(除非全局命名空间被删除,这是一个解释器结束事件(,因此永远不会调用跟踪,并且创建的命令会无限期地保留。

如果我们这样重写您的创建:

proc create {} {
global all
# Double quotes just because of the highlighting here
set all [uplevel "#0" atomselect top all]
}

那么atomselect在全局范围内运行(并且创建的命令应该持久存在(,但您的所有其他代码都在过程中。

更恰当地说,应该这样做:

proc create {} {
global all
# Double quotes just because of the highlighting here
set all [uplevel "#0" [list atomselect top all]]
# Or this, to better show that we're talking about running a command:
#   set all [uplevel "#0" [list 
#       atomselect top all]]
}

当您在中传递带有空格(或其他元语法字符(的参数时,这一点变得更加重要,因为list不仅生成列表,还生成无替换命令。(事实上,构建命令和命令片段绝对是list命令的主要用途之一;普通数据存储和操作的实际列表往往是用其他命令创建的。(

相关内容

最新更新