I need to 'upvar' variables from a source'd file (and I don't know their names)
Let's say I have a TCL file containing:
set v 7
Now this:
proc p {} {
source t.tcl
}
p
puts $v
# => can't read "v": no such variable
The file t.tcl must not be changed.
I also can't have knowledge about the content of t.tcl. Let's assume it's passed as an env variable. How do I make v visible at the top level?
For the code you posted, change the definition of
pto:or (to specifically connect the variable to the top level:
(Note: the syntax highlighter thinks the string
"#0 v v"is a comment, but it isn't.)Members of the
envare global, so you won't have that problem there.However, if you don't know the names of the variables it's less simple, because you can't
upvara variable once it exists.Is it possible to evaluate the
sourcecommand in the top level, like this:(Again, not a comment.)
Or do you have to evaluate it in
p's level?Documentation: proc, puts, set, source, uplevel, upvar
Documentation for env
(Note: the 'Hoodiecrow' mentioned in the comments is me, I used that nick earlier.)