1uplevel(n) Tcl Built-In Commands uplevel(n)
2
3
4
5______________________________________________________________________________
6
8 uplevel - Execute a script in a different stack frame
9
11 uplevel ?level? arg ?arg ...?
12_________________________________________________________________
13
15 All of the arg arguments are concatenated as if they had been passed to
16 concat; the result is then evaluated in the variable context indicated
17 by level. Uplevel returns the result of that evaluation.
18
19 If level is an integer then it gives a distance (up the procedure call‐
20 ing stack) to move before executing the command. If level consists of
21 # followed by a number then the number gives an absolute level number.
22 If level is omitted then it defaults to 1. Level cannot be defaulted
23 if the first command argument starts with a digit or #.
24
25 For example, suppose that procedure a was invoked from top-level, and
26 that it called b, and that b called c. Suppose that c invokes the
27 uplevel command. If level is 1 or #2 or omitted, then the command
28 will be executed in the variable context of b. If level is 2 or #1
29 then the command will be executed in the variable context of a. If
30 level is 3 or #0 then the command will be executed at top-level (only
31 global variables will be visible).
32
33 The uplevel command causes the invoking procedure to disappear from the
34 procedure calling stack while the command is being executed. In the
35 above example, suppose c invokes the command
36 uplevel 1 {set x 43; d}
37 where d is another Tcl procedure. The set command will modify the
38 variable x in b's context, and d will execute at level 3, as if called
39 from b. If it in turn executes the command
40 uplevel {set x 42}
41 then the set command will modify the same variable x in b's context:
42 the procedure c does not appear to be on the call stack when d is exe‐
43 cuting. The info level command may be used to obtain the level of the
44 current procedure.
45
46 Uplevel makes it possible to implement new control constructs as Tcl
47 procedures (for example, uplevel could be used to implement the while
48 construct as a Tcl procedure).
49
50 The namespace eval and apply commands offer other ways (besides proce‐
51 dure calls) that the Tcl naming context can change. They add a call
52 frame to the stack to represent the namespace context. This means each
53 namespace eval command counts as another call level for uplevel and
54 upvar commands. For example, info level 1 will return a list describ‐
55 ing a command that is either the outermost procedure call or the outer‐
56 most namespace eval command. Also, uplevel #0 evaluates a script at
57 top-level in the outermost namespace (the global namespace).
58
60 As stated above, the uplevel command is useful for creating new control
61 constructs. This example shows how (without error handling) it can be
62 used to create a do command that is the counterpart of while except for
63 always performing the test after running the loop body:
64 proc do {body while condition} {
65 if {$while ne "while"} {
66 error "required word missing"
67 }
68 set conditionCmd [list expr $condition]
69 while {1} {
70 uplevel 1 $body
71 if {![uplevel 1 $conditionCmd]} {
72 break
73 }
74 }
75 }
76
78 apply(n), namespace(n), upvar(n)
79
81 context, level, namespace, stack frame, variables
82
83
84
85Tcl uplevel(n)