【发布时间】:2013-09-03 07:28:32
【问题描述】:
我遇到了我无法理解的eval 命令的奇怪行为。当我尝试使用 eval 运行名称存储在变量中的命令时,我得到了奇怪的结果。
数组mCallBackCont在insert下的值为::postLayRep1||mainTableView sendToLoads,所以*:
>>set mCallBackCont(insert)
::postLayRep1||mainTableView sendToLoads
其中::postLayRep1||mainTableView 是具有sendToLoads 公共方法的类的对象
当我尝试执行以下操作之一时:
eval {::postLayRep1||mainTableView sendToLoads}
eval ::postLayRep1||mainTableView sendToLoads
eval "::postLayRep1||mainTableView sendToLoads"
eval $mCallBackCont(insert)
eval "mCallBackCont(insert)"
我得到了正确的行为,但是当我使用时
eval {$mCallBackCont(insert)}
我得到错误:
invalid command name "::postLayRep1||mainTableView sendToLoads"
当我尝试使用不带参数的常规 proc 时:
>>proc test_proc {} {return}
>>set a test_proc
>>eval {$a}
一切正常,但是当我添加参数时,也会发生同样的情况:
>>proc test_proc {val} {puts $val}
>>set a [list test_proc 1]
test_proc 1
>>eval {$a}
invalid command name "test_proc 1"
由于 eval 命令是我正在使用的库中代码的一部分,我无法更改它,我唯一能确定的是 mCallBackCont(insert) 的值。库中的代码是:
if { [catch {eval {$mCallBackCont(insert) [namespace tail $this] $type $name $n $redraw}} e] } {
error "Wrong number of arguments for the procedure \"$mCallBackCont(insert)\". Should be \"table type name num redraw\"."
}
为什么
eval {$var}对 procs 有效,但对类的方法无效(我猜这与 proc 是一个 1 字命令,而方法更复杂的事实有关)?我可以通过什么方式设置
mCallBackCont(insert)的值以使其正常工作?
* - 我试图将值放在mCallBackCont(insert) 中,既是一个列表,也是一个由""包围的字符串
【问题讨论】:
标签: tcl