uplevel(n)
π uplevel(n) β bestie edition β¨
okay so this is the Tcl Built-In Commands page for uplevel, and it's literally the girlboss of stack frames. let's get into it π«Ά
β¨ NAME β¨
uplevel β execute a script in a different stack frame π«
πΈ SYNOPSIS πΈ
uplevel ?level? arg ?arg ...?
π DESCRIPTION π
okay so first thing: all of the arg arguments get concatenated together, exactly as if you'd passed them to concat. then the result of that gets evaluated in the variable context that level points at. and uplevel returns the result of that evaluation. no cap. π¦
now, level. if level is an integer, it's a distance β like, how far up the procedure calling stack you wanna scoot before running the command. if level is a # followed by an integer, then that's an absolute level instead. and if you just... don't give a level at all? it defaults to 1. π
β οΈ but bestie listen βlevelcannot be defaulted if the first command argument is an integer or starts with#. so watch out for that, fr fr.
so like, picture this: procedure a got invoked from top-level, and a called b, and b called c. and c invokes the uplevel command. here's the vibe check:
- if
levelis1or#2or omitted β the command runs in the variable context ofbπΈ - if
levelis2or#1β the command runs in the variable context ofaβ¨ - if
levelis3or#0β the command runs at top-level (only global variables will be visible) π
and here's the main character energy part: uplevel causes the invoking procedure to disappear from the procedure calling stack while the command is being executed. iconic, honestly.
so in that same example, say c invokes this:
uplevel 1 {set x 43; d}
where d is another Tcl procedure. the set command is gonna modify the variable x in b's context, and d will execute at level 3, like it was called from b. and if d in turn executes this:
uplevel {set x 42}
then that set command modifies the same variable x in b's context. because c does not appear to be on the call stack at all while d is executing. it's giving ghosting but make it stack frames π
oh and you can use the info level command to grab the level of the current procedure. handy π
uplevel is what makes it possible to implement new control constructs as Tcl procedures β like, for example, uplevel could be used to implement the while construct as a Tcl procedure. obsessed π
also important: the namespace eval and apply commands offer other ways (besides procedure calls) that the Tcl naming context can change. they add a call frame to the stack to represent the namespace context. which means each namespace eval command counts as another call level for uplevel and upvar commands. so 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). noted, bestie π
π EXAMPLE π
like we said, uplevel is useful for creating new control constructs. this example shows how (without error handling) it can be used to create a do command that's the counterpart of while β except it always performs the test after running the loop body:
proc do {body while condition} {
if {$while ne "while"} {
error "required word missing"
}
set conditionCmd [list expr $condition]
while {1} {
uplevel 1 $body
if {![uplevel 1 $conditionCmd]} {
break
}
}
}
slay. periodt. π
π¦ SEE ALSO π¦
π KEYWORDS π
- context
- level
- namespace
- stack frame
- variable
and that's the whole thing, bestie!! π this was the uplevel(n) page from Tcl β the Tcl Built-In Commands manual β so go forth and uplevel responsibly. mwah ππ
β¨
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- 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