local

local is a routine in Jim that creates a routine that only exists until the current routine ends.

See Also

itcl::local
lambda
A way to have a local routine in standard Tcl.

Description

A local variable exists in the local namespace of the current routine, and is deleted when the routine ends.

From the manual:

local cmd ?arg...?

First, local evaluates the given command. The return value must be the name of an existing command, which is marked as having local scope. 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 is kept rather than deleted, and may be called via upcall. The previous command is restored when the current routine ends. See upcall for more details.

In this example, a local routine is created. Note that the routine 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
    ...
}

JAL - 2021-06-07 20:26:59

It seems to me that local could be useful for other commands besides proc. For example, rename or set. A nifty use for this would be if you are implementing a proc to be shared with others, but it requires a helper proc. local could allow you to implement the helper as a "nested proc" without worrying about a name collision, supposing local will automatically handle renaming and reverting.

The case for local set ... might be if you wanted to temporarily override debug flag for the lifetime of one method invocation, but wanted to revert to he old value when exiting.