Updated 2012-10-31 23:24:18 by AMG

See itcl::local

A local variable exists only as long as the proc that created it is running. It lives within the proc's stack frame. When the proc returns, the local variable "goes out of scope" and is destroyed.

Jim Tcl supports the local command to limit the lifetime of commands. From the manual:
local cmd ?arg...?

First, local evaluates cmd with the given arguments. The return value must be the name of an existing command, which is marked as having local scope. This means that when the current procedure exits, the specified command is deleted. This can be useful with lambda, local procedures or to automatically close a filehandle.

In addition, if a command already exists with the same name, the existing command will be kept rather than deleted, and may be called via upcall. The previous command will be restored when the current procedure exits. See upcall for more details.

In this example, a local procedure is created. Note that the procedure continues to have global scope while it is active.
proc outer {} {
  # proc ... returns "inner" which is marked local
  local proc inner {} {
    # will be deleted when 'outer' exits
  }
  inner
  ...
}