The tcl docs provide the following example on how to use the upvar command:
proc add2 name {
upvar $name x
set x [expr {$x + 2}]
}
Inside this procedure, x acts as a reference to the variable name which belongs to the scope outside of add2.
I don't understand why the first argument to upvar has to be provided with the dollar sign, and the second argument not. Shouldn't it simply be upvar name x, since we are referencing name rather than its value?
Here's an example of how add2 might be used:
Here add2 is called with its parameter called "name" set to the value "fred". If the code then did
upvar name xit would be operating on a variable literally called "name" but we need it to operate on the variable called "fred" so we write$nameto get the value "fred" from the parameter called "name".