I'm getting lost on scoping variables in Tcl. I've got two procs I've written and stored in a file that I want to call in another script I'm writing. In my sourced file (which I'll call bar.tcl
), I'm using upvar
to define a bunch of variables at the beginning of my file that I plan to call within the procs I've defined within bar.tcl
, which I'll call bar1
and bar2
I want to call the functions within bar.tcl
from my code within foo.tcl
. My functions work fine if I'm just interacting with bar.tcl in tclsh, but as soon as I call bar1 or bar2 from within a function in foo.tcl, I get an error can't read "alpha": no such variable
It seems I'm using upvar
incorrectlys. Is this even the right tool for the job? What can I do so that I can achieve the following?
- define a bunch of variables at the beginning of
bar.tcl
that I want to use in both procsbar1
andbar2,
and - call
bar1
andbar2
from within procs defined infoo.tcl
Here's my code in foo.tcl
:
# foo.tcl
source bar.tcl
proc foo {fooValues} {
foreach fooValue $fooValues {
set myNewValue [bar1 $fooValue]
return $myNewValue
}
}
set myOldValues {5 10 15 20 25}
foo myOldValues
And here's my code in bar.tcl
:
set a apples
set b bananas
set c cherries
set d dates
proc bar1 {bar} {
upvar a alpha
upvar b bravo
upvar c charlie
upvar d delta
set myReturnValue "$bar $alpha $bravo $charlie $delta"
return $myReturnValue
}
proc bar2 {bar} {
upvar a alpha
upvar b bravo
upvar c charlie
upvar d delta
set myReturnValue "$alpha $bravo $charlie $delta $bar"
return $myReturnValue
}