env name add var... ;# in the caller, who calls the callee with $name env name get ;# in the calleeOnly scalar variables can be wrapped. The callee gets copies of the variables which he can change at will, without influencing the "originals" in the caller. Both features mirror those of OS environment variables. }
proc env {_name mode args} {
upvar 1 $_name var
switch -- $mode {
add {foreach n $args {lappend var $n [uplevel 1 set $n]}}
get {foreach {n val} $var {uplevel 1 [list set $n $val]}}
default {error "bad mode $mode, use add or get"}
}
}#-- Testing (the example should leave no question open): proc caller arg {
set foo 42
set bar grill
env e add arg foo bar
callee 123 $e
}
proc callee {x e} {
env e get
foreach name [info vars] {
lappend res $name=[set $name]
}
set res
}
puts [caller hello!]if 0 {which returns: x=123 {e=arg hello! foo 42 bar grill} foo=42 bar=grill arg=hello!See also closures.NEM: In particular, see Simple Closures and Objects, which does a similar thing, but using dict with to restore the environment in the new scope.Category Example | Arts and crafts of Tcl-Tk programming}
