set
Read and write variables.
Syntax
tcl
set varName ?newValue?Parameters
- varName: Name of the variable, optionally namespace-qualified (e.g.,
::foo::bar) - newValue: Optional new value to assign
Description
The set command reads and writes variables:
- With one argument: returns the current value of the variable
- With two arguments: sets the variable to the new value and returns it
- Creates a new variable if one does not already exist
Supported Features
- Namespace-qualified variable names: Variables can be accessed using namespace qualifiers like
::foo::bar. The resolution follows standard TCL namespace rules. - Frame-local variables: For unqualified names, frame-local (procedure-local) storage is used.
- Variable traces: Both read and write traces are fired appropriately.
- Variable creation: Setting a non-existent variable creates it automatically.
Error Handling
- Wrong number of arguments:
wrong # args: should be "set varName ?newValue?" - Reading non-existent variable:
can't read "varName": no such variable
Limitations
- Array element syntax: TCL's standard
set arr(index) valuesyntax for arrays is not supported. A variable name likearr(foo)is treated as a literal scalar variable name rather than accessing elementfooof arrayarr.
Examples
Setting and getting a variable
Output
Using set to read a variable
Output
Namespace-qualified names
Output
Variable creation
Output
