1

我遇到了eval我无法理解的命令的奇怪行为。当我尝试使用 eval 运行名称存储在变量中的命令时,我得到了奇怪的结果。

数组mCallBackCont的值::postLayRep1||mainTableView sendToLoads低于insert,因此*

>>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 是一个单字命令的事实有关,而方法更复杂)?

  • 我可以通过什么方式设置 的值mCallBackCont(insert)以使其正常工作?


* - 我试图将值放在mCallBackCont(insert)一个列表和一个字符串中""

4

1 回答 1

4

首先,Tcl 命令名称中可以包含空格。或者实际上几乎任何其他字符;唯一需要注意的是:s,因为::它是名称空间分隔符,并且前导::很好,因为它只是意味着它是一个完全限定的名称。

因此,它::postLayRep1||mainTableView sendToLoads是一个合法但不寻常的命令名称。如果将名称放入变量中,则可以使用对该变量的读取,就像它是命令名称一样:

$theVariableContainingTheCommandName

在这方面,使用数组元素并没有什么特别之处。

现在,如果你想把它当作一个脚本,你可以eval像这样传递它:

eval $theVariableContainingTheScript

您遇到的真正问题是您正在做:

eval {$theVariableContainingTheScript}

这被定义为与只做完全相同:

$theVariableContainingTheScript

那永远不会做你想要的。查看导致问题的代码:

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\"."
}

在这种情况下,变量中的值必须是命令的名称,而不仅仅是脚本片段。最简单的解决方法是创建一个绑定额外参数的别名:

interp alias {} callBackForInsert {} ::postLayRep1||mainTableView sendToLoads

然后你可以callBackForInsert像调用::postLayRep1||mainTableView第一个参数一样使用它sendToLoads。它实际上是一个命名的部分应用程序。或者,您可以使用帮助程序:

proc callBackForInsert args {
    return [uplevel 1 {::postLayRep1||mainTableView sendToLoads} $args]
}

但在这个简单的案例中,这既丑陋又慢。8.6 中更好的是使用tailcall

proc callBackForInsert args {
    tailcall ::postLayRep1||mainTableView sendToLoads {*}$args
}

但这仍然比别名慢,因为额外的堆栈帧操作的开销。


但是,如果可以的话,最好的解决方法是更改​​库,使其使用这样的回调(假设 Tcl 8.5 或更高版本):

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\"."
}

可以简化为:

if { [catch {{*}$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在现代 Tcl 代码中使用它。a {*}-扩展几乎总是更接近预期。

如果您坚持使用 8.4 但可以更改库代码,您可以这样做:

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这使用了在通过 Tcl 脚本评估引擎反馈它们之前连接其参数的事实。


别名、扩展tailcall和(未在此答案中使用)合奏的组合让您可以用很少的代码做很棒的事情,允许复杂的参数重新混合,而不必用大量的帮助程序加载自己。

于 2013-09-03T08:20:34.073 回答