我们如何发现tcl环境中存在哪些包,它们存储在哪里,以及如何包含新的包



我的目标是在tcl脚本中执行log-base2,但它对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. 执行";包名称";返回";系统控制台zlib TclOO tcl::tommath tcl";并且这不包括:tcl::mathfunc。为什么?这个列表太小了
  2. 为什么log(2.71(返回";无效命令名";错误,但expr日志(2.71(工作
  3. 为什么expr::tcl::mathfunc::log(2.71(失败,但::tcl:::mathfunc::rand有效?这两个不是mathfunc包的一部分吗

查找包

  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
    

    (这里的"virtual"实际上是一个,因为log函数返回一个浮点数。与自定义函数有一些小的技术差异。(

  2. 请注意,括号在上面的调用中已经消失它们只是表达式的语法。从技术上讲,表达式是嵌入Tcl中的自己的小语言(这反过来又嵌入了Tcl(。

    rand的重写最终只是删除了括号,因为它不需要参数。

最新更新