我正在尝试计算 proc 所属的包,以便验证包版本
说我有包裹
some::package 1.0
哪个有 proc
some::package::some_proc
给定 some::package::some_proc 我希望能够获得 1.0
我不能保证命名空间与它所属的包同名
非常感谢任何帮助
我正在尝试计算 proc 所属的包,以便验证包版本
说我有包裹
some::package 1.0
哪个有 proc
some::package::some_proc
给定 some::package::some_proc 我希望能够获得 1.0
我不能保证命名空间与它所属的包同名
非常感谢任何帮助
从技术上讲,如果不提供自己的package require
. Tcl 中的命名空间、包和文件没有链接,就像它们在 Python 或 Java 中一样。
如果您知道所有 proc 都是静态定义的(例如,没有动态代码生成),您可以替换package require
为您自己的版本并枚举调用前后的所有 proc 并将名称放入某个注册表中。
你想验证什么?
如果你想验证某种 API,你可以查看 Tcllib 中的pluginmgr包。
Conventionally, but not universally, you get the name of a package from the name of the namespace containing the command in question:
set pkg [namespace qualifiers [namespace which $theCommand]]
# Be aware, this produces the empty string for global commands
However, not all packages do this; some older packages do not put their public commands into a namespace (for historical/back-compatibility reasons). In that case, searching the documentation is about the only thing you can do.
Once you've got the package name, you can determine the currently-loaded version of the package with package present
:
set version [package present $pkg]
This only works if the package has previously been package require
d, of course. You can also use package versions
to determine what other versions of the package are available in your environment; though Tcl will only load one version of a package into an interpreter context, you can have many different ones installed without (necessarily) having problems.
您正在寻找namespace current
:
% namespace eval some::package {
proc some_proc {} { puts [namespace current] }
}
% some::package::some_proc
::some::package