0

我正在尝试计算 proc 所属的包,以便验证包版本

说我有包裹

some::package 1.0

哪个有 proc

some::package::some_proc

给定 some::package::some_proc 我希望能够获得 1.0

我不能保证命名空间与它所属的包同名

非常感谢任何帮助

4

3 回答 3

2

从技术上讲,如果不提供自己的package require. Tcl 中的命名空间、包和文件没有链接,就像它们在 Python 或 Java 中一样。

如果您知道所有 proc 都是静态定义的(例如,没有动态代码生成),您可以替换package require为您自己的版本并枚举调用前后的所有 proc 并将名称放入某个注册表中。

你想验证什么?

如果你想验证某种 API,你可以查看 Tcllib 中的pluginmgr包。

于 2013-08-31T18:15:35.453 回答
0

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 required, 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.

于 2013-08-31T19:43:41.890 回答
0

您正在寻找namespace current

% namespace eval some::package {
    proc some_proc {} { puts [namespace current] }
}
% some::package::some_proc
::some::package
于 2013-08-31T17:40:07.040 回答