VarName | Name of variable to find/create in caller. |
Var | Name of variable to use locally. |
DefaultValue | Optional. Value to set a newly created variable to. Defaults to empty string. |
The following example shows use of UpvarX in a proc that will create a variable and then immediately read it (a case where upvar will not work).
proc MySetZeroIfEmpty {VarName} {
UpvarX $VarName Var
if {[IsEmpty $Var]} {
set Var 0
}
return $Var
}
% unset -nocomplain NewVariable
% MySetZeroIfEmpty NewVariable
0
% puts $NewVariable
0