Tcl variable Command
last modified April 3, 2025
The Tcl variable command declares variables in namespaces and
procedures. It ensures variables exist in the current scope before use.
This prevents potential issues with variable resolution.
Basic Definition
The variable command creates one or more variables in the current
namespace. It's particularly important for procedures that need to access
global or namespace variables.
Syntax: variable ?name value?.... It can declare multiple variables
at once. Without a value, it just declares the variable's existence.
Basic Variable Declaration
This shows the simplest usage of variable to declare a variable.
variable name "John Doe" puts $name
This declares a variable name with the value "John Doe". The
puts command then prints the variable's value. This is similar
to set but with namespace awareness.
Global Variable Access
The variable command is essential for accessing global variables
inside procedures.
set ::counter 0
proc increment {} {
variable ::counter
incr ::counter
}
increment
puts $::counter
Here we declare a global variable ::counter and access it inside
the increment procedure using variable. This ensures
proper variable resolution.
Namespace Variables
variable is crucial for working with namespace variables.
namespace eval math {
variable PI 3.14159
proc area {radius} {
variable PI
expr {$PI * $radius * $radius}
}
}
puts [math::area 5]
This creates a namespace math with a constant PI.
The area procedure accesses PI using variable.
This maintains proper namespace encapsulation.
Multiple Variable Declaration
variable can declare multiple variables in one command.
namespace eval config {
variable width 800 height 600 title "App"
proc show {} {
variable width height title
puts "Config: $width x $height, $title"
}
}
config::show
This declares three variables in the config namespace at once.
The show procedure accesses them using a single variable
command. This is more efficient than separate declarations.
Variable Without Initialization
variable can declare a variable without assigning a value.
proc process {} {
variable result
set result "Operation completed"
return $result
}
puts [process]
Here we declare result without initialization inside a procedure.
The variable is later assigned a value with set. This pattern is
useful for procedure-local variables.
Combining with upvar
variable can be combined with upvar for advanced
variable handling.
namespace eval outer {
variable data "Important information"
proc access {} {
variable data
upvar 1 data localdata
puts "Accessed: $localdata"
}
}
outer::access
This demonstrates combining variable with upvar to
create an alias to a namespace variable. The access procedure
can work with the variable through a local name.
Best Practices
- Namespace use: Always use
variablefor namespace variables. - Global access: Prefer
variableoverglobalfor globals. - Early declaration: Declare variables at procedure start.
- Explicit names: Use fully qualified names when needed.
- Documentation: Comment namespace variables for clarity.
This tutorial covered the Tcl variable command with practical
examples showing its usage in different scenarios.
Author
List all Tcl Tutorials.