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.

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:

Output with uplevel 2

Output of the program without uplevel statement

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;}

Leave a Reply

Your email address will not be published. Required fields are marked *