Version 6 of local

Updated 2021-04-24 20:11:02 by pooryorick

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