TCL奇怪的怪癖

发布于 2024-09-27 08:35:10 字数 767 浏览 4 评论 0原文

所以我对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南月 2024-10-04 08:35:10

交互式 tclsh 会话将尝试执行未知命令(例如rm)。您不能指望在非交互式脚本执行中或如您所发现的过程中出现此行为。

我看不到 tclsh man 中记录了这一点页面,但 未知 手册页可以。另请参阅 Tcl wiki 上的 tclsh 页面。在交互式 tclsh 会话中,您可以通过键入以下内容来查看 unknown 的作用:

info body unknown

[update]

引用“Tcl 和 Tk 中的实用编程”:

unknown 命令还提供了一些其他便利。仅当您直接键入命令时才使用这些。一旦执行进入过程或未交互使用 Tcl shell,它们就会被禁用。便利的功能包括自动执行程序、命令历史记录和命令缩写。如果无法从脚本库加载命令实现,则会按顺序尝试这些选项。

An interactive tclsh session will try to exec an unknown command (such as rm). 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:

info body unknown

[update]

Quoting from "Practical Programming in Tcl and Tk":

The unknown command provides a few other conveniences. These are used only when you typing commands directly. They are disabled once execution enters a procedure or if the Tcl shell is not being used interactively. The convenience features are automatic execution of programs, command history, and command abbreviation. These options are tried, in order, if a command implementation cannot be loaded from a script library.

白芷 2024-10-04 08:35:10

请注意,这也可以通过变量 tcl_interactive 以编程方式进行测试,如果 Tcl 通过交互式 shell 运行,则该变量为“1”,否则为“0”。该变量也是可设置的,因此可以启动交互式 shell,然后[set tcl_interactive 0],然后继续。此时,人们会失去诸如

  • % 命令提示符
  • proc name / 命令名补全等功能(即:无法键入 [pu "xyz"] 并获得效果键入 [puts "xyz"],如交互式 shell)
  • 自动“shell-out”以让外部命令完成请求(如本原始问题中的“rm”),
  • 也许还有其他命令。 .

  • 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

  • the % command prompt
  • proc name / command name completion (i.e.: can't type [pu "xyz"] and get the effect of typing [puts "xyz"], like an interactive shell)
  • automatic "shell-out" to have external commands complete a request (like the "rm" in this original question)
  • and perhaps others...

  • ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文