【问题标题】:How do we find what packages exist in our tcl environment and where they are stored and how to include new packages?我们如何找到我们的 tcl 环境中存在哪些包以及它们存储在哪里以及如何包含新包?
【发布时间】:2020-08-05 13:23:50
【问题描述】:

我的目标是在我的 tcl 脚本中执行 log base 2,但它提出了一些关于 tcl 如何工作的问题。我需要做这些事情:

  1. 在我的 tcl 环境中查找可用包列表
  2. 查找包中可用的程序列表
  3. 像我们在 Shell 中使用 -h 或 --help 开关一样查找过程的“信息”或“描述”
  4. 如何将新包添加到我们的 tcl 环境中?是否有 tcl 的软件包可以像 Python(我们使用 pip)一样下载?

现在我尝试自己执行一些命令,跟踪如下:

% info log
error: unknown or ambiguous subcommand "log": must be args, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars
    while executing
"info log"
% log(2.71)
error: invalid command name "log(2.71)"
    while executing
"log(2.71)"
% expr log(2.71)
0.9969486348916096
% info ::tcl::mathfunc
error: unknown or ambiguous subcommand "::tcl::mathfunc": must be args, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars
    while executing
"info ::tcl::mathfunc"
% info ::tcl::mathfunc::log
error: unknown or ambiguous subcommand "::tcl::mathfunc::log": must be args, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars
    while executing
"info ::tcl::mathfunc::log"
% expr ::tcl::mathfunc::log(2.71)
error: missing operand at _@_
in expression "_@_::tcl::mathfunc::log(2..."
    (parsing expression "::tcl::mathfunc::log(2...")
    invoked from within
"expr ::tcl::mathfunc::log(2.71)"
% info 
error: wrong # args: should be "info subcommand ?arg ...?"
    while executing
"info "
% info library
C:/intelFPGA/18.1/quartus/bin64/tcl8.6
% package names
systemconsole zlib TclOO tcl::tommath Tcl
% ::tcl::mathfunc::rand
0.6648586465347831
% info ::tcl::mathfunc::rand
error: unknown or ambiguous subcommand "::tcl::mathfunc::rand": must be args, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars
    while executing
"info ::tcl::mathfunc::rand"

这条踪迹让我感到困惑的是:

  1. 执行“包名称”返回“systemconsole zlib TclOO tcl::tommath Tcl”,这不包括 ::tcl::mathfunc。这是为什么?这个列表太小了!
  2. 为什么 log(2.71) 返回“invalid command name”错误,但 expr log(2.71) 有效?
  3. 为什么 expr ::tcl::mathfunc::log(2.71) 失败但 ::tcl::mathfunc::rand 有效?两者都不是 mathfunc 包的一部分吗?

【问题讨论】:

  • 从技术上讲,log(2.71) 是一个有效的函数名。将括号放在这样的名称中只是非常不寻常,因为在使用关联数组时会令人困惑。
  • 另外,尝试运行info commands tcl::mathfunc::* 以获取所有函数实现命令的列表。

标签: tcl


【解决方案1】:

查找包

  1. Tcl 等待构建包列表,直到您请求一个它当前不知道名称的包。 catch {package require thereisnosuchpackage} 应该大部分填充列表。

    但是,有些包被存储为 Tcl 模块并且从未进入列表中。它们使用更有效的加载机制,但在格式上有所限制。 tcl::tm::path list 会给出一个可以找到它们的目录列表,并且包的名称和版本会根据这些目录构建到一个文件中。

    我不喜欢没有办法整齐地获得这些模块的列表,即使只是为了维护和发现目的。

表达式中的函数

  1. expr 命令重写log(1.23) 函数的调用来自:

    expr { log(1.23) }
    

    调用:

    expr { [tcl::mathfunc::log [expr { 1.23 }]] }
    

    最终相当于:

    expr { [tcl::mathfunc::log 1.23] }
    

    这反过来实际上等同于:

    tcl::mathfunc::log 1.23
    

    (这里的“虚拟”是一个真的,因为日志函数返回一个浮点数。与自定义函数存在一些细微的技术差异。)

  2. 请注意,上述调用中的括号已消失; 它们只是表达式的语法。从技术上讲,表达式是它们自己嵌入在 Tcl 中的小语言(而后者又嵌入了 Tcl)。

    rand 的重写只是删除了括号,因为它不带任何参数。

【讨论】:

  • 我可以看到tcl在很多方面与C不同
  • 我很感兴趣。您能否提供一个不会出现在package names 输出中的模块示例?
猜你喜欢
  • 2014-04-25
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多