Main Content

Tcl - uplevel to run code at calling level

Archive - Originally posted on "The Horse's Mouth" - 2009-10-24 09:43:24 - Graham Ellis

Tcl procs (they're Tcl's functions) can pass information back via the return command, and they can access data in the top level of code via the global commands. Incoming parameters can be passed in 'by name' so that any values altered within the proc are also altered in the calling code - there's an example here using the upvar command and another here.

A further facility - uplevel - allows you to run a command in the scope of the proc that called your code. It can be useful for setting an extra return value, but it's also a command that allows all sorts of horrors - so be careful. And remember that any variables that you reference in the uplevel command that are prefixed with a $ will be interpretted / substituted before the uplevel command is even run - so will be from the local scope. There's an example of uplevel in use here if you want to see what I mean!