Version 1 of scope

Updated 2004-08-22 03:56:13

Scope is a context within which to resolve names to values.

Tcl has many scopes:

namespace is an hierarchal scope for resolving names of commands and variables. The variable command references a variable name in namespace scope.

global scope is a distinguished namespace scope, equivalent to namespace :: in some interpreter

hidden scope is a per-interpreter scope for interp hidden commands

proc local scope is the context within which all otherwise unqualified variable names in a process are defined.

call frame scope is the context of some call frame in the current evaluation. Proc local is a special case of a call frame scope, being the nearest enclosing call frame scope.


In summary, there are two major kinds of scoping in tcl - dynamic and lexical (or static) scope.

dynamic scope is accessed by default inside a proc, or by means of upvar with unqualified names, and is preferred for variable name resolution.

static or lexical scope is accessed explicitly by namespace encoded names, the global operator, and implicitly by command invocation and by the variable operator. Command resolution prefers dynamic scope.