无法在Tcl中使用upvar将变量传递给过程

时间:2011-10-21 09:01:51

标签: tcl proc-object upvar

我需要一个能够从调用者的命名空间访问,读取和更改变量的过程。该变量称为_current_selection。我试图用几种不同的方式使用upvar来做,但没有任何效果。 (我编写了一个小测试过程来测试upvar机制)。以下是我的尝试:


致电proc:

select_shape $this _current_selection

PROC:

proc select_shape {main_gui var_name} {
    upvar  $var_name curr_sel
    puts " previously changed:  $curr_sel"
    set curr_sel [$curr_sel + 1]
}

我的第二次尝试:

致电proc:

select_shape $this

PROC:

proc select_shape {main_gui} {
    upvar  _current_selection curr_sel
    puts " previously changed:  $curr_sel"
    set curr_sel [$curr_sel + 1]
}

在所有尝试中,一旦它在代码中到达此区域,就会显示can't read "curr_sel": no such variable

我做错了什么?

修改

该函数的调用是通过bind命令进行的:

$this/zinc bind current <Button-1> [list select_shape $this _current_selection]

一开始我认为没关系。但也许确实如此。

3 个答案:

答案 0 :(得分:4)

我相信bind命令在全局命名空间中运行,因此需要找到变量。这可能有效:

$this/zinc bind current <Button-1> \
    [list select_shape $this [namespace current]::_current_selection]

答案 1 :(得分:3)

要使upvar工作,变量必须存在于您调用它的范围内。请考虑以下内容:

proc t {varName} {
   upvar $varName var
   puts $var
}

#set x 1
t x

如果按原样运行它,您将收到报告的错误,取消注释 set x 1 行,它将起作用。

答案 2 :(得分:0)

在下面的示例中,我试图涵盖从其他命名空间更改变量的大多数变体。它100%适合我。也许它会有所帮助。

proc select_shape {main_gui var_name} {
    upvar  $var_name curr_sel
    puts " previously changed:  $curr_sel"
    incr curr_sel
}

namespace eval N {
  variable _current_selection 1
  variable this "some_value"

  proc testN1 {} {
    variable _current_selection
    variable this
    select_shape $this _current_selection
    puts " new: $_current_selection"
  }

  # using absolute namespace name
  proc testN2 {} {
    select_shape [set [namespace current]::this] [namespace current]::_current_selection
    puts " new: [set [namespace current]::_current_selection]"
  }

  select_shape $this _current_selection
  puts " new: $_current_selection"
}

N::testN1
N::testN2

#-------------------------------------
# Example with Itcl class
package require Itcl

itcl::class C {
  private variable _current_selection 10

  public method testC {} {
    select_shape $this [itcl::scope _current_selection]
    puts " new: $_current_selection"
  }
}

set c [C #auto]
$c testC