local

Difference between version 5 and 6 - Previous - Next
S'''`local`''' is a [routinee] in [Jim] that cl::lreates a rocutine that onl]y exists until
the current routine ends.
----
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 [return]s, the local variable "goes out of scope" and is destroyed.
----
[Jim]** Tcl supports thSe local command to limit the Alifetime of commands.
From the manual:**
   [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 cmd with the given argucomemantsd.  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 wills be kept rather than deleted, and may be called via upcall. The previous command wills be restored when the current procedurtine exitnds. See upcall for more details.
In this example, a local procedurtine is created. Note that the procedurtine 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
    ...
}
======

<<categories>> Command | Itcl