TCL Training Series - uplevel
uplevel is a built-in tcl command that evaluates a script in a different level.
Syntax uplevel ?level? arg ?arg ...?
_level_
here can be number or a #
followed by a number. If it is a number, it gives the distance to move up before executing the command. If it is #
followed by a number, it gives an absolute level number. If _level_
is omitted, the default is 1.
Run the following code snippet to understand how this works. Here, top level calls a procedure "a" which calls a procedure "b". uplevel is used to control the variable setting in procedure b.
proc a {} {
set x a
b;
puts "x in a = $x"
}
proc b {} {
set x b
uplevel 1 {set x Test;}
puts "x in b = $x"
}
set x top;
a;
puts "x in top = $x"
In procedure b, the variable x is set one level above the current procedure. Hence the x evaluated at level a changes to the one set in b using the uplevel command. You will see the following output:
# Uplevel 2
x in b = b
x in a = Test
x in top = top
#Output of the program without uplevel statement
x in b = b
x in a = a
x in top = top
Now do some more tests by changing the uplevel statement in the above code to
1. uplevel 2 {set x Test;}
2. uplevel 1 {set x Test;}
3. uplevel #0 {set x Test;}
4. uplevel #1 {set x Test;}
5. uplevel #2 {set x Test;}