英文:
How to hide a command (not disable) in tclsh
问题
在tclsh中,我想隐藏一个不能直接调用但可以以某种方式调用的命令。有没有一种好的方法或我可以研究的思路。
英文:
In tclsh I would like to hide a command that cannot be called directly but can be called in a certain way.
Is there a good way to do this or an idea that I can look into.
答案1
得分: 5
你可以隐藏命令,只要它们在全局命名空间中(我知道这很丑陋!)使用interp hide
命令。然后,你可以使用interp invokehidden
来调用它们,使用interp expose
来取消隐藏:
% proc hideme args {puts "HIDDEN: args=$args"}
% hideme foo
HIDDEN: args=foo
% interp hide {} hideme
% hideme foo
invalid command name "hideme"
% interp invokehidden {} hideme foo
HIDDEN: args=foo
% interp hidden {}
hideme
% interp expose {} hideme
% interp invokehidden {} hideme foo
invalid hidden command name "hideme"
% hideme foo
HIDDEN: args=foo
这是Tcl中的安全机制的一部分,但只有在命令被父解释器隐藏时才会起作用。在上面的情况下,从技术上讲,这并不是因为你正在从自己那里隐藏命令,而且可以再次找到隐藏的命令。没有安全边界就没有安全性。({}
是空字符串,是相对于自身的当前解释器的“名称”)。
安全解释器会隐藏许多标准命令,默认情况下会使解释器无法与操作系统交互。
英文:
You can hide commands, provided they are in the global namespace (I know that's ugly!) using the interp hide
command. You then use interp invokehidden
to call them, and interp expose
to undo the hiding:
% proc hideme args {puts "HIDDEN: args=$args"}
% hideme foo
HIDDEN: args=foo
% interp hide {} hideme
% hideme foo
invalid command name "hideme"
% interp invokehidden {} hideme foo
HIDDEN: args=foo
% interp hidden {}
hideme
% interp expose {} hideme
% interp invokehidden {} hideme foo
invalid hidden command name "hideme"
% hideme foo
HIDDEN: args=foo
This is part of the security mechanisms in Tcl, but only when the command is hidden by a parent interpreter. In the case above, it technically isn't because you're hiding from yourself and can find the hidden command again. There's no security without a security boundary. (The {}
is the empty string and is the "name" of the current interpreter with respect to itself.)
Safe interpreters hide many standard commands, leaving the interpreter unable to interact with the OS by default.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论