Tcl scope and namespaces
last modified November 24, 2026 Tcl 8.6
Every Tcl variable and command lives in a namespace. The main script runs in the
global namespace, written ::, and every procedure call creates a
fresh local scope for its arguments and local variables. Namespaces add named
containers on top of that, so a larger program can keep its procedures and
variables apart instead of filling one flat global space.
This article moves from the procedure scope to real modules: global and local
variables, the global and variable commands,
upvar and uplevel, namespaces with their own state,
exports and imports, qualified names and a small two-module program. The
namespace command page is the full reference for
the subcommands used here.
Global and local scope
A procedure runs in its own scope: variables created with set
inside it exist only while it runs. A local variable with the same name as a
global one shadows it, and the global is still reachable through its qualified
name ::name.
set name "global Jane"
proc show {} {
set name "local Jane"
puts "inside: $name"
puts "global: $::name"
}
show
puts "outside: $name"
The procedure sets a local name and prints it, then prints the
global one through ::name. After the call the global value is
unchanged, because the two variables are different. Reading $name
inside the procedure without setting it first would be an error: Tcl never falls
back to a global variable by itself.
$ tclsh scope.tcl inside: local Jane global: global Jane outside: global Jane
The global and variable commands
To read or change a global variable from a procedure, declare it with
global. The variable command does the same for a
variable in the procedure's namespace: inside a procedure in the global
namespace it links ::total, and inside a namespace it links that
namespace's variable.
set total 0
proc add {n} {
global total
incr total $n
}
proc reset {} {
variable total
set total 0
}
add 5
add 7
puts "total: $total"
reset
puts "after reset: $total"
puts "global view: $::total"
add declares global total once and then uses
total like a local variable. reset achieves the same
with variable total, which is the form that also works inside a
namespace. Both procedures change the same storage, as the output shows.
Declaring the link at the top of the procedure keeps the dependency visible; the
global page and the
variable page compare the two commands.
$ tclsh global.tcl total: 12 after reset: 0 global view: 0
upvar and uplevel
upvar creates a local alias for a variable in another scope, which
is how Tcl passes arguments by reference. uplevel evaluates a script
in another scope, so a procedure can run code as if the caller had written it.
proc swap {aName bName} {
upvar 1 $aName a $bName b
set tmp $a
set a $b
set b $tmp
}
set x 1
set y 2
swap x y
puts "x=$x y=$y"
proc in_caller {} {
uplevel 1 {puts "caller says: $message"}
}
proc caller {} {
set message "Hello there"
in_caller
}
caller
swap receives two variable names, links them to local names with
upvar 1, and exchanges their values, so the caller's variables
change. The second example uses uplevel 1 to evaluate a script in
caller, where message exists. Both commands are
powerful and easy to misuse: return a value unless a procedure really must
modify the caller's state. The upvar page explains the
level syntax.
$ tclsh upvar.tcl x=2 y=1 caller says: Hello there
namespace eval and namespace variables
namespace eval creates a namespace and runs a script inside it.
Variables declared there with variable belong to the namespace, not
to the global scope, and are reached from outside with a qualified name.
namespace eval counter {
variable count 0
variable step 1
}
proc counter::bump {} {
variable count
variable step
incr count $step
return $count
}
puts [counter::bump]
puts [counter::bump]
puts "value: $counter::count"
puts "namespace: [namespace eval counter {namespace current}]"
The namespace holds count and step. The procedure
counter::bump declares both with variable and updates
the counter; the qualified name $counter::count reads the same
storage from the main script. A namespace is a named container for commands and
variables, independent of any particular procedure call, which is why the count
survives between the two calls.
$ tclsh counter.tcl 1 2 value: 2 namespace: ::counter
Procedures inside a namespace
Procedures are usually defined inside namespace eval, where they
can see the namespace variables directly and where their names are created in
that namespace. The fully qualified name is then
::geometry::area, while from the global namespace the shorter
geometry::area is enough.
namespace eval geometry {
variable pi 3.14159
proc area {r} {
variable pi
return [expr {$pi * $r * $r}]
}
proc circumference {r} {
variable pi
return [expr {2 * $pi * $r}]
}
}
puts "area r=2: [format %.2f [geometry::area 2]]"
puts "circle r=3:[format %.2f [geometry::circumference 3]]"
puts "pi: $geometry::pi"
puts "procs: [namespace eval geometry {info procs}]"
Both procedures declare variable pi and use it as a constant. The
last line runs info procs inside the namespace and shows only its
two procedures. Because the value lives in the namespace, every call shares the
same pi without passing it as an argument, and changing
$geometry::pi would affect both procedures at once.
$ tclsh procs.tcl area r=2: 12.57 circle r=3:18.85 pi: 3.14159 procs: area circumference
Export and import
namespace export marks commands as the public interface of a
namespace; namespace import copies those commands into the current
namespace under their short names. Unexported commands stay private and can
still be called by their qualified names.
namespace eval text {
namespace export shout whisper
proc shout {s} {
return [string toupper $s]
}
proc whisper {s} {
return [string tolower $s]
}
proc helper {s} {
return " $s "
}
}
puts "exported: [namespace eval text {namespace export}]"
namespace import text::*
puts [shout "hello there"]
puts [whisper "QUIET PLEASE"]
puts "helper visible: [expr {[info commands helper] ne ""}]"
namespace forget text::*
puts "after forget: [expr {[info commands shout] ne ""}]"
Only shout and whisper are exported, so
namespace import text::* imports just those two and
helper remains invisible, as the info commands test
shows. Importing shortens calls but hides where a command comes from, and
namespace forget removes the imported names again. Qualified names
are clearer when several modules provide similar commands.
$ tclsh export.tcl exported: shout whisper HELLO THERE quiet please helper visible: 0 after forget: 0
Qualified names and namespace path
A qualified name starts with :: and lists namespaces separated by
::; namespace qualifiers and
namespace tail split such a name.
namespace path tells a namespace where to look for commands that
are not defined locally, a lighter alternative to importing them.
namespace eval text {
namespace export shout
proc shout {s} {
return [string toupper $s]
}
}
namespace eval app {
namespace path {::text}
proc greet {name} {
return [shout "hello $name"]
}
}
puts "greeting: [app::greet Jane]"
puts "parent: [namespace qualifiers ::app::greet]"
puts "tail: [namespace tail ::app::greet]"
puts "path: [namespace eval app {namespace path}]"
puts "exists: [namespace exists ::app]"
puts "commands: [info commands ::text::*]"
The app namespace puts ::text on its path, so
shout resolves there even though it was never imported. The helpers
report the container and the last component of a qualified name, and
info commands confirms that the procedure really lives in
::text. Path lookup searches the listed namespaces in order and
finds their commands whether or not they are exported, so a path is a broader
tool than an import.
$ tclsh path.tcl greeting: HELLO JANE parent: ::app tail: greet path: ::text exists: 1 commands: ::text::shout
A small two-module example
The final program splits a small application into two namespaces.
store owns the data and exports three commands;
report imports them and exports one command that renders a table.
Neither namespace exposes its internals to the main script.
namespace eval store {
namespace export add items total
variable data {}
proc add {name price} {
variable data
lappend data [list $name $price]
}
proc items {} {
variable data
return $data
}
proc total {} {
variable data
set sum 0
foreach item $data {
set sum [expr {$sum + [lindex $item 1]}]
}
return $sum
}
}
namespace eval report {
namespace import ::store::*
namespace export render
proc render {} {
set width 0
foreach item [items] {
set len [string length [lindex $item 0]]
if {$len > $width} {
set width $len
}
}
set out {}
foreach item [items] {
lappend out [format "%-*s %8.2f" $width [lindex $item 0] \
[lindex $item 1]]
}
lappend out [string repeat - [expr {$width + 9}]]
lappend out [format "%-*s %8.2f" $width TOTAL [total]]
return [join $out \n]
}
}
store::add Keyboard 49.90
store::add Monitor 189.00
store::add {USB cable} 7.50
puts [report::render]
puts "items in store: [llength [store::items]]"
store keeps its list in a namespace variable and changes it only
through add, so the representation stays private.
report imports the exported interface and uses it inside
render; the main script calls store::add and
report::render and never touches a variable. This is the usual way
to grow a Tcl program: one namespace per module, a small exported surface, and
qualified calls at the boundaries.
$ tclsh modules.tcl Keyboard 49.90 Monitor 189.00 USB cable 7.50 ------------------ TOTAL 246.40 items in store: 3
Source
The namespace page of the Tcl 8.6 manual documents every subcommand
used above, including namespace path,
namespace export and namespace ensemble. The
upvar, uplevel and global pages cover the
scope commands in detail.
This article followed a script from one global namespace to two cooperating
modules: local scope, explicit globals, references with upvar,
namespace state, exports, imports and qualified names. Continue with
Tcl string processing or
Tcl dictionaries to put these modules to work on real
data.
Author
List all Tcl Tutorials.