Artifact e48a49eb65f7fceca5ab32f0cbfe0baa357c0577bfa069c8fc7fddb41eb10e2b:
- File doc/markdown/uplevel.md — part of check-in [bb08aafe0a] at 2025-07-15 19:41:45 on branch tip-700 — some non-change cosmetics to make the ctags implementation in Geany 2.1 partly happy again (user: Torsten, size: 3783) [annotate] [blame] [check-ins using]
CommandName: uplevel ManualSection: n Version: unknown TclPart: Tcl TclDescription: Tcl Built-In Commands Links:
- apply(n)
- namespace(n)
- upvar(n) Keywords:
- context
- level
- namespace
- stack frame
- variable Copyright:
- Copyright (c) 1993 The Regents of the University of California.
- Copyright (c) 1994-1997 Sun Microsystems, Inc. ---
Name
uplevel - Execute a script in a different stack frame
Synopsis
::: {.synopsis} ::: [uplevel]{.cmd} [level=?+ arg= ?+arg]{.optdot} :::
Description
All of the fIargfR arguments are concatenated as if they had been passed to fBconcatfR; the result is then evaluated in the variable context indicated by fIlevelfR. fBUplevelfR returns the result of that evaluation.
If fIlevelfR is an integer then it gives a distance (up the procedure calling stack) to move before executing the command. If fIlevelfR consists of fB#fR followed by a integer then the level gives an absolute level. If fIlevelfR is omitted then it defaults to fB1fR. fILevelfR cannot be defaulted if the first fIcommandfR argument is an integer or starts with fB#fR.
For example, suppose that procedure fBafR was invoked from top-level, and that it called fBbfR, and that fBbfR called fBcfR. Suppose that fBcfR invokes the fBuplevelfR command. If fIlevelfR is fB1fR or fB#2fR or omitted, then the command will be executed in the variable context of fBbfR. If fIlevelfR is fB2fR or fB#1fR then the command will be executed in the variable context of fBafR. If fIlevelfR is fB3fR or fB#0fR then the command will be executed at top-level (only global variables will be visible).
The fBuplevelfR command causes the invoking procedure to disappear from the procedure calling stack while the command is being executed. In the above example, suppose fBcfR invokes the command
uplevel 1 {set x 43; d}
where fBdfR is another Tcl procedure. The fBsetfR command will modify the variable fBxfR in fBbfR's context, and fBdfR will execute at level 3, as if called from fBbfR. If it in turn executes the command
uplevel {set x 42}
then the fBsetfR command will modify the same variable fBxfR in fBbfR's context: the procedure fBcfR does not appear to be on the call stack when fBdfR is executing. The fBinfo levelfR command may be used to obtain the level of the current procedure.
Uplevel makes it possible to implement new control constructs as Tcl procedures (for example, fBuplevelfR could be used to implement the fBwhilefR construct as a Tcl procedure).
The fBnamespace evalfR and fBapplyfR 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. This means each fBnamespace evalfR command counts as another call level for fBuplevelfR and fBupvarfR commands. For example, fBinfo level 1fR will return a list describing a command that is either the outermost procedure call or the outermost fBnamespace evalfR command. Also, fBuplevel #0fR evaluates a script at top-level in the outermost namespace (the global namespace).
Example
As stated above, the fBuplevelfR command is useful for creating new control constructs. This example shows how (without error handling) it can be used to create a fBdofR command that is the counterpart of fBwhilefR except for always performing 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
}
}
}