TCL奇怪的怪癖
所以我对TCL编程方式非常陌生,缺乏经验。我编写了一个脚本,该脚本调用其他人编写的过程,首先删除输出文件。然后它会执行我编写的一些附加逻辑。
我将逻辑移到第二个过程中,其中一堆立即崩溃了(即 rm 命令)。
据我所知,中央执行内一行上的第一个程序(proc 定义后面的文本)可以在没有“exec”命令的情况下正常执行。但是,如果将其移动到过程中,它现在需要一个“exec”命令。
谁能帮我解释一下TCL为什么会这样?
例如
proc helloworld {} {
puts "hi"
}
#works
rm my_file
helloworld
..
proc helloworld {} {
#doesn't work
rm my_file
puts "hi"
}
helloworld
..
proc helloworld {} {
#works
eval rm my_file
puts "hi"
}
helloworld
..
proc helloworld {} {
#works
file delete my_file
puts "hi"
}
helloworld
*请注意,这种奇怪的行为可能特定于我将脚本提供给vmd 的程序,该程序有自己内置的TCL 行为。也许您可以在您的回复中指出这是否也是其他口译员的标准?
So I am very new and inexperienced to the ways of TCL programming. I wrote a script that calls a proc written by someone else, first removing the output file. It then does some additional logic I wrote.
I moved the logic into a second proc and instantly a bunch of it broke (namely the rm commands).
From what I can tell, the first program on a line inside the central execution (the text following proc definitions) executes normally without an "exec" command. However, if you move it inside a proc, it now needs an "exec" command.
Can anyone explain to me why TCL behaves this way?
e.g.
proc helloworld {} {
puts "hi"
}
#works
rm my_file
helloworld
..
proc helloworld {} {
#doesn't work
rm my_file
puts "hi"
}
helloworld
..
proc helloworld {} {
#works
eval rm my_file
puts "hi"
}
helloworld
..
proc helloworld {} {
#works
file delete my_file
puts "hi"
}
helloworld
*Note this weird behavior may be specific to the program I'm feeding the script to vmd, which has its own built in TCL behavior. Perhaps in your responses you can indicate if this is standard for other interpreters as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
交互式 tclsh 会话将尝试
执行
未知命令(例如rm
)。您不能指望在非交互式脚本执行中或如您所发现的过程中出现此行为。我看不到 tclsh man 中记录了这一点页面,但 未知 手册页可以。另请参阅 Tcl wiki 上的 tclsh 页面。在交互式 tclsh 会话中,您可以通过键入以下内容来查看
unknown
的作用:[update]
引用“Tcl 和 Tk 中的实用编程”:
An interactive tclsh session will try to
exec
an unknown command (such asrm
). You cannot count on this behaviour in non-interactive script execution or, as you've discovered, in procs.I can't see that this is documented in the tclsh man page, but the unknown man page does. See also the tclsh page on the Tcl wiki. In an interactive tclsh session, you can see what
unknown
does by typing:[update]
Quoting from "Practical Programming in Tcl and Tk":
请注意,这也可以通过变量 tcl_interactive 以编程方式进行测试,如果 Tcl 通过交互式 shell 运行,则该变量为“1”,否则为“0”。该变量也是可设置的,因此可以启动交互式 shell,然后[set tcl_interactive 0],然后继续。此时,人们会失去诸如
Note this is programatically testable too, via the variable tcl_interactive, which is "1" if the Tcl is being run via an interactive shell, and "0" if not. The variable is also settable, so one could start an interactive shell, then [set tcl_interactive 0], and continue on. At this point, one loses such features as