| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH uplevel n "" Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
uplevel \- Execute a script in a different stack frame |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBuplevel \fR?\fIlevel\fR?\fI arg \fR?\fIarg ...\fR? |
| 16 |
.BE |
| 17 |
.SH DESCRIPTION |
| 18 |
.PP |
| 19 |
All of the \fIarg\fR arguments are concatenated as if they had |
| 20 |
been passed to \fBconcat\fR; the result is then evaluated in the |
| 21 |
variable context indicated by \fIlevel\fR. \fBUplevel\fR returns |
| 22 |
the result of that evaluation. |
| 23 |
.PP |
| 24 |
If \fIlevel\fR is an integer then |
| 25 |
it gives a distance (up the procedure calling stack) to move before |
| 26 |
executing the command. If \fIlevel\fR consists of \fB#\fR followed by |
| 27 |
a number then the number gives an absolute level number. If \fIlevel\fR |
| 28 |
is omitted then it defaults to \fB1\fR. \fILevel\fR cannot be |
| 29 |
defaulted if the first \fIcommand\fR argument starts with a digit or \fB#\fR. |
| 30 |
.PP |
| 31 |
For example, suppose that procedure \fBa\fR was invoked |
| 32 |
from top-level, and that it called \fBb\fR, and that \fBb\fR called \fBc\fR. |
| 33 |
Suppose that \fBc\fR invokes the \fBuplevel\fR command. If \fIlevel\fR |
| 34 |
is \fB1\fR or \fB#2\fR or omitted, then the command will be executed |
| 35 |
in the variable context of \fBb\fR. If \fIlevel\fR is \fB2\fR or \fB#1\fR |
| 36 |
then the command will be executed in the variable context of \fBa\fR. |
| 37 |
If \fIlevel\fR is \fB3\fR or \fB#0\fR then the command will be executed |
| 38 |
at top-level (only global variables will be visible). |
| 39 |
.PP |
| 40 |
The \fBuplevel\fR command causes the invoking procedure to disappear |
| 41 |
from the procedure calling stack while the command is being executed. |
| 42 |
In the above example, suppose \fBc\fR invokes the command |
| 43 |
.PP |
| 44 |
.CS |
| 45 |
\fBuplevel\fR 1 {set x 43; d} |
| 46 |
.CE |
| 47 |
.PP |
| 48 |
where \fBd\fR is another Tcl procedure. The \fBset\fR command will |
| 49 |
modify the variable \fBx\fR in \fBb\fR's context, and \fBd\fR will execute |
| 50 |
at level 3, as if called from \fBb\fR. If it in turn executes |
| 51 |
the command |
| 52 |
.PP |
| 53 |
.CS |
| 54 |
\fBuplevel\fR {set x 42} |
| 55 |
.CE |
| 56 |
.PP |
| 57 |
then the \fBset\fR command will modify the same variable \fBx\fR in \fBb\fR's |
| 58 |
context: the procedure \fBc\fR does not appear to be on the call stack |
| 59 |
when \fBd\fR is executing. The \fBinfo level\fR command may |
| 60 |
be used to obtain the level of the current procedure. |
| 61 |
.PP |
| 62 |
\fBUplevel\fR makes it possible to implement new control |
| 63 |
constructs as Tcl procedures (for example, \fBuplevel\fR could |
| 64 |
be used to implement the \fBwhile\fR construct as a Tcl procedure). |
| 65 |
.PP |
| 66 |
The \fBnamespace eval\fR and \fBapply\fR commands offer other ways |
| 67 |
(besides procedure calls) that the Tcl naming context can change. |
| 68 |
They add a call frame to the stack to represent the namespace context. |
| 69 |
This means each \fBnamespace eval\fR command |
| 70 |
counts as another call level for \fBuplevel\fR and \fBupvar\fR commands. |
| 71 |
For example, \fBinfo level 1\fR will return a list |
| 72 |
describing a command that is either |
| 73 |
the outermost procedure call or the outermost \fBnamespace eval\fR command. |
| 74 |
Also, \fBuplevel #0\fR evaluates a script |
| 75 |
at top-level in the outermost namespace (the global namespace). |
| 76 |
.SH EXAMPLE |
| 77 |
As stated above, the \fBuplevel\fR command is useful for creating new |
| 78 |
control constructs. This example shows how (without error handling) |
| 79 |
it can be used to create a \fBdo\fR command that is the counterpart of |
| 80 |
\fBwhile\fR except for always performing the test after running the |
| 81 |
loop body: |
| 82 |
.PP |
| 83 |
.CS |
| 84 |
proc do {body while condition} { |
| 85 |
if {$while ne "while"} { |
| 86 |
error "required word missing" |
| 87 |
} |
| 88 |
set conditionCmd [list expr $condition] |
| 89 |
while {1} { |
| 90 |
\fBuplevel\fR 1 $body |
| 91 |
if {![\fBuplevel\fR 1 $conditionCmd]} { |
| 92 |
break |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
.CE |
| 97 |
.SH "SEE ALSO" |
| 98 |
apply(n), namespace(n), upvar(n) |
| 99 |
.SH KEYWORDS |
| 100 |
context, level, namespace, stack frame, variable |
| 101 |
.\" Local Variables: |
| 102 |
.\" mode: nroff |
| 103 |
.\" End: |