【问题标题】:How to find the file paths of all namespace loaded in an application using tcl/TK under Unix?如何在 Unix 下使用 tcl/TK 查找应用程序中加载的所有命名空间的文件路径?
【发布时间】:2021-04-24 02:44:55
【问题描述】:

对于现有流程,运行某些脚本作业时会加载一大堆命名空间。 但是,如果我想检查和跟踪某个命名空间中某些命令的使用情况,我需要找到某个命名空间的脚本路径。 有什么办法可以得到吗?特别是,我说的是黄金时段脚本。

【问题讨论】:

标签: path namespaces tcl


【解决方案1】:

从技术上讲,命名空间没有脚本路径。但我们可以做一些足够接近的事情:

proc report_current_file {call code result op} {
    if {$code == 0} {
        # If the [proc] call was successful...
        set cmd [lindex $call 1]
        set qualified_cmd [uplevel 1 [list namespace which $cmd]]
        set file [file normalize [info script]]
        puts "Defined $qualified_cmd in $file"
    }
}
trace add execution proc leave report_current_file

如果您有动态创建过程的过程,这并不完美——当前文件可能是错误的——但幸运的是,大多数代码不是这样做的。

另一个可能对您有用的选项是使用tcl::unsupported::getbytecode,它以机器可读格式(字典)生成大量信息。其中一条信息是sourcefile 键。这是一个在我的机器上以交互方式运行的示例:

% parray tcl_platform
tcl_platform(byteOrder)     = littleEndian
tcl_platform(engine)        = Tcl
tcl_platform(machine)       = x86_64
tcl_platform(os)            = Darwin
tcl_platform(osVersion)     = 20.2.0
tcl_platform(pathSeparator) = :
tcl_platform(platform)      = unix
tcl_platform(pointerSize)   = 8
tcl_platform(threaded)      = 1
tcl_platform(user)          = dkf
tcl_platform(wordSize)      = 8
% dict get [tcl::unsupported::getbytecode proc parray] sourcefile
/opt/local/lib/tcl8.6/parray.tcl

请注意,必须已经定义了该过程才能使其正常工作。如果 Tcl 对代码所在的文件感到困惑(因为动态编程技巧),那么该密钥就不存在了。

【讨论】:

  • 那本字典里还有各种各样的东西。它拥有的确切密钥(或它们的数据格式)不是 Tcl API 的一部分,我们保证在即使是次要版本之间也能维护……但我不认为 sourcefile 会消失。这太好了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 2010-12-05
  • 2019-01-18
  • 2022-06-15
  • 2013-01-01
  • 1970-01-01
相关资源
最近更新 更多