Frage

I have two embedded Tcl-scripts (main.tcl, secondary.tcl) which have to pass variables back and forth. Depending on the results of main.tcl before running secondary.tcl certain predefined variables in secondary.tcl have to be altered. Then secondary.tcl has to pass some variable to main.tcl to define how to continue.

Example:

#main.tcl

# Here there is first some script which give some results
# Let's say for now result==0 or 1 and some variable "res" 

if { result==0 } {
    set var1 $res
} else {
    set var2 $res
}

# Depending on which of the variables is set, the corresponding
# variable should be altered in secondary.tcl

source /path/to/secondary.tcl

# Now the result in secondary.tcl has to be inserted in this tcl-script
set res_sec $result_secondary

# Rest of script is executed

The secondary tcl-script looks like this

#secondary.tcl

set var1 some_value1
set var2 some_value2

# Rest of script is executed

set result_secondary some_result  ;# This variable has to be passed to main.tcl

# End of script

I hope it's clear what I'm trying to do. I know that in some tcl-scripts they use 'parameter' and then using: secondary.tcl -parameters "var1 $res" results in only changing var1 in secondary.tcl. But I couldn't find out how to do that.

War es hilfreich?

Lösung

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).

Andere Tipps

Not sure, never really tried this. I think you need to save your variables to a file and read them back in the other script. If you save them as Tcl code (set foo bar and so on) you can just source the file in the other script and they automagically become live variables in that script.

# script0.tcl:
set foo bar
...
set f [open script1.tcl w]
puts $f "set foo $foo"
close $f


# script1.tcl:
set foo bar


# script2.tcl:
source script1.tcl

if {$foo eq "bar"} {
    ...
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top