upvar(3tcl)
๐ upvar โ bestie, we're linking variables across stack frames โจ
okay so real talk: this is upvar, a Tcl built-in command, and it lives in section 3tcl of the manual. It's giving "portal gun for variable names" and honestly? iconic. Let's go ๐ซถ
โจ SYNOPSIS โจ
upvar ?level? otherVar myVar ?otherVar myVar ...?
๐ธ DESCRIPTION ๐ธ
So here's the tea ๐: this command arranges for one or more local variables in the current procedure to refer to variables in an enclosing procedure call, or to global variables. Basically it's a group chat invite between frames.
level can have any of the forms that are permitted for the uplevel command, and you can totally omit it โ it defaults to 1, no cap.
For each otherVar argument, upvar makes the variable by that name in the procedure frame given by level (or at global level, if level is #0) accessible in the current procedure by the name given in the corresponding myVar argument. Two names, one variable, zero drama ๐
Key facts, bestie, don't skip these:
- The variable named by
otherVarneed not exist at the time of the call. It'll be created the first timemyVaris referenced, just like an ordinary variable. Chill. - There must not exist a variable by the name
myVarat the timeupvaris invoked. That's a hard no. ๐ myVaris always treated as the name of a variable, not an array element.- An error is returned if the name looks like an array element, such as
a(b). otherVarmay refer to a scalar variable, an array, or an array element. Flexible queen โจupvarreturns an empty string. That's it. That's the return value. ๐
๐ฆ why you'd even want this ๐ฆ
The upvar command simplifies the implementation of call-by-name procedure calling, and it also makes it easier to build new control constructs as Tcl procedures. For example, consider the following procedure:
proc add2 name {
upvar $name x
set x [expr {$x + 2}]
}
If add2 is invoked with an argument giving the name of a variable, it adds two to the value of that variable. Main character behaviour tbh ๐ฅ
Although add2 could have been implemented using uplevel instead of upvar, upvar makes it simpler for add2 to access the variable in the caller's procedure frame. Work smarter, not harder.
๐ namespaces, the plot twist ๐
namespace eval is another way (besides procedure calls) that the Tcl naming context can change. It adds a call frame to the stack to represent the namespace context. This means each namespace eval command counts as another call level for uplevel and upvar commands.
For example, info 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 the outermost namespace (the global namespace). iykyk ๐
๐ the unset situation ๐
If an upvar variable is unset (e.g. x in add2 above), the unset operation affects the variable it is linked to, not the upvar variable. Not me making you read that twice ๐ญ
There is no way to unset an upvar variable except by exiting the procedure in which it is defined. That's the rules, bestie.
However โ and this is the slay part โ it is possible to retarget an upvar variable by executing another upvar command. Reroute the link, babes. ๐
๐ฎ TRACES AND UPVAR ๐ฎ
okay so upvar interacts with traces in a straightforward but possibly unexpected manner. Vibe check: it's not broken, it's just โจquirkyโจ.
If a variable trace is defined on otherVar, that trace will be triggered by actions involving myVar. However, the trace procedure will be passed the name of myVar, rather than the name of otherVar. So the output of the following code will be โlocalVarโ rather than โoriginalVarโ:
proc traceproc { name index op } {
puts $name
}
proc setByUpvar { name value } {
upvar $name localVar
set localVar $value
}
set originalVar 1
trace add variable originalVar write traceproc
setByUpvar originalVar 2
If otherVar refers to an element of an array, then the element name is passed as the second argument to the trace procedure. This may be important information in case of traces set on an entire array. so like, write that down ๐
๐ง EXAMPLE ๐ง
A decr command that works like incr except it subtracts the value from the variable instead of adding it:
proc decr {varName {decrement 1}} {
upvar 1 $varName var
incr var [expr {-$decrement}]
}
subtract, but make it fashion ๐
๐ SEE ALSO ๐
๐ KEYWORDS ๐
context, frame, global, level, namespace, procedure, upvar, variable
and that's the whole page, bestie ๐ฅฐ this one's from Tcl, and it's the upvar(3tcl) page โ go forth and link those variables, you absolute icon ๐โจ๐๐ฆ
๐ฆ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1997 Sun Microsystems, Inc
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms