NAME
UpvarX — Do upvar. If made new variable, then set to default.
SYNOPSIS
UpvarX VarName Var ?DefaultValue?
DESCRIPTION
Return Value
None.
Arguments
VarNameName of variable to find/create in caller.
VarName of variable to use locally.
DefaultValueOptional. Value to set a newly created variable to. Defaults to empty string.
More Info
This command was written to be able to write procs that have behavior similar to incr and append. When you use those commands, you can give it the name of a variable that does not yet exist and one will be created for you (and initialized to a default if you choose).

For information regarding exceptions / errors, see here.
EXAMPLES

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
SEE ALSO
UpvarExistingOrDie, VarExistsInCaller
KEYWORDS
unknown, variable