exchanging variables between interpreters

Arjen Markus (15 june 2010) If you use different interpreters, you may not only want to invoke procedures from one interpreter in the other, but you may also want to share variables (or at least make the values available in the other interpreter on demand). Here is a small example of how you can do that.

The trick is: traces. With traces you can change the value of a variable, even if you just read it.

The program below merely illustrates the process, it should be made more general if you really want to use it.

#     Exchange variables between interps

set var "Aha"

proc getVar {name1 name2 op} {
    interp eval foo [list set $name1 [set $name1]]

interp create foo
interp eval foo {
    trace add variable ::var read getVar
    set ::var {xxx}
interp alias foo getVar {} getVar

interp eval foo {
    puts [getVar ::var {} {}]
    puts "Var = $::var"

See also MOST.