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 otherVar need not exist at the time of the call. It'll be created the first time myVar is referenced, just like an ordinary variable. Chill.
  • There must not exist a variable by the name myVar at the time upvar is invoked. That's a hard no. ๐Ÿ™ˆ
  • myVar is 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).
  • otherVar may refer to a scalar variable, an array, or an array element. Flexible queen โœจ
  • upvar returns 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms