As long as you source
the scripts into the same interpreter context, they'll share variables by default. (Indeed, if you don't want sharing you need to take extra steps, such as using namespaces or separate interpreters.) Thus, all you need to do is to decide what variables are expected going in, and what variables going out.
In your case, you're conditionally setting either var1
or var2
going into secondary.tcl
, and always setting result_secondary
going out to main.tcl
. Going out is easy — you've got that part right already! — but your secondary.tcl
needs a little adjustment. What it needs is a ways to detect if the variables have been set (if they aren't, you can adjust behaviour as you desire, perhaps using a default or whatever). That's easy:
if {[info exists var1]} {
puts "var1 was set to $var1"
} else {
set var1 "default var1 value"
}
If the scripts are in different interpreters, things get more complicated because you've got to get past the isolation enforcement. (That's even more important when you're dealing with multiple processes, or even multiple computers.) Yet I think the simplest thing is what you're requiring here; if I'm wrong, edit your question to be clearer (or ask another one).