upvar(n) Tcl Built-In Commands upvar(n) NAME upvar - Create link(1,2) to variable in(1,8) a different stack frame SYNOPSIS upvar ?level? otherVar myVar ?otherVar myVar ...? DESCRIPTION This command arranges for one or more local variables in(1,8) the current procedure to refer to variables in(1,8) an enclosing procedure call or to global variables. Level may have any of the forms permitted for the uplevel command, and may be omitted if(3,n) the first letter of the first otherVar isn't # or a digit (it defaults to 1). For each otherVar argument, upvar makes the variable by that name in(1,8) the procedure frame given by level (or at global level, if(3,n) level is #0) accessible in(1,8) the current procedure by the name given in(1,8) the corresponding myVar argu- ment. The variable named(5,8) by otherVar need not exist at the time(1,2,n) of the call; it will be created the first time(1,2,n) myVar is referenced, just like an ordinary variable. There must not exist a variable by the name myVar at the time(1,2,n) upvar is invoked. MyVar is always treated as the name of a variable, not an array element. Even if(3,n) the name looks like an array element, such as a(b), a regular variable is created. Other- Var may refer to a scalar variable, an array, or an array element. Upvar returns an empty string. The upvar command simplifies the implementation of call-by-name proce- dure calling and also makes it easier to build new control constructs as Tcl procedures. For example, consider the following procedure: proc(5,n) add2 name { upvar $name x set(7,n,1 builtins) x [expr(1,3,n) $x+2] } Add2 is invoked with an argument giving the name of a variable, and it adds two to the value of that variable. Although add2 could have been implemented using uplevel instead of upvar, upvar makes it simpler for add2 to access(2,5) the variable in(1,8) the caller's procedure frame. namespace eval is another way (besides procedure calls) that the Tcl naming context can change. It adds a call frame to the stack to repre- sent the namespace context. This means each namespace eval command counts as another call level for uplevel and upvar commands. For exam- ple, info(1,5,n) level 1 will return a list describing a command that is either the outermost procedure call or the outermost namespace eval command. Also, uplevel #0 evaluates a script at top-level in(1,8) the out- ermost namespace (the global namespace). If an upvar variable is unset (e.g. x in(1,8) add2 above), the unset opera- tion affects the variable it is linked to, not the upvar variable. There is no way to unset an upvar variable except by exiting the proce- dure in(1,8) which it is defined. However, it is possible to retarget an upvar variable by executing another upvar command. Traces and upvar Upvar interacts with traces in(1,8) a straightforward but possibly unex- pected manner. If a variable trace(3x,n,3x _nc_tracebits) is defined on otherVar, that trace(3x,n,3x _nc_tracebits) will be triggered by actions involving myVar. However, the trace(3x,n,3x _nc_tracebits) pro- cedure will be passed the name of myVar, rather than the name of other- Var. Thus, the output of the following code will be localVar rather than originalVar: proc(5,n) traceproc { name index op } { puts(3,n) $name } proc(5,n) setByUpvar { name value } { upvar $name localVar set(7,n,1 builtins) localVar $value } set(7,n,1 builtins) originalVar 1 trace(3x,n,3x _nc_tracebits) variable originalVar w trace- proc(5,n) setByUpvar originalVar 2 } If otherVar refers to an element of an array, then variable traces set(7,n,1 builtins) for the entire array will not be invoked when myVar is accessed (but traces on the particular element will still be invoked). In particu- lar, if(3,n) the array is env(1,3), then changes made to myVar will not be passed to subprocesses correctly. SEE ALSO global(n), namespace(n), uplevel(n), variable(n) KEYWORDS context, frame, global, level, namespace, procedure, variable Tcl upvar(n)