Question

I need a procedure that will be able to access, read and change a variable from the namespace of the caller. The variable is called _current_selection. I have tried to do it using upvar in several different ways, but nothing worked. (I've written small test proc just to test the upvar mechanism). Here are my attempts:


call to 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]
}

For my second attempt:

call to 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]
}

In all the attempts, once it reaches this area in the code it says can't read "curr_sel": no such variable

What am I doing wrong?

EDIT:

The call for the function is made from a bind command:

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

at start I thought that it doesn't matter. but maybe It does.

Was it helpful?

Solution

I believe that bind commands operate in the global namespace, so that's where the variable is expected to be found. This might work:

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

OTHER TIPS

for upvar to work the variable must exist in the scope that you are calling it in. consider the following:

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

#set x 1
t x

If you run it as it is you'll get the error you are reporting, uncomment the set x 1 line and it will work.

In the example below I've tried to cover the most variants of changing variables from other namespace. It 100% works for me. Maybe it will help.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top