variable(n)
π variable(n) β Tcl Built-In Commands π
okay bestie, gather round, because variable is THE command that makes namespaces stop being scary. no cap. it's a Tcl built-in and it's here to create and initialize a namespace variable. that's the whole vibe. periodt. β¨
πΈ SYNOPSIS πΈ
variable name
variable ?name value...?
β¨ DESCRIPTION β¨
so basically π
this command is normally used inside a namespace eval command to create one or more variables inside a namespace. each variable name gets initialized with value. and the value for the last variable? optional. she's free-spirited like that. π¦
here's the tea on what happens:
- if a variable name doesn't exist, it gets created. in that case, if value is specified, it's assigned to the newly created variable. if no value is specified, the new variable is left undefined (not undefined behaviour, just literally no value, she's a mystery π).
- if the variable already exists, it's set to value if value is specified, or left unchanged if no value is given.
ngl, normally name is unqualified (meaning it doesn't include the names of any containing namespaces) and the variable gets created in the current namespace. but if name includes any namespace qualifiers, then the variable is created in the specified namespace. main character chooses her own home. π
lowkey important detail: if the variable is not defined, it will be visible to the namespace which command, but not to the info exists command. the way that it's sort of there but info exists says no?? iconic. gaslighting. π₯
now, if the variable command is executed inside a Tcl procedure, it creates local variables linked to the corresponding namespace variables (and therefore these variables are listed by info vars). so in this way, variable resembles the global command β except the global command resolves variable names with respect to the global namespace instead of the current namespace of the procedure. subtle but crucial, bestie. π
if any values are given, they're used to modify the values of the associated namespace variables. and if a namespace variable doesn't exist, it is created and optionally initialized. cute, right?
β οΈ big warning energy: a name argument cannot reference an element within an array. instead, name should reference the entire array, and the initialization value should be left off. after the variable has been declared, elements within the array can be set using ordinarysetorarraycommands. do NOT try to be clever here, i'm begging. π
π EXAMPLES π
create a variable in a namespace:
namespace eval foo {
variable bar 12345
}
create an array in a namespace:
namespace eval someNS {
variable someAry
array set someAry {
someName someValue
otherName otherValue
}
}
access variables in namespaces from a procedure:
namespace eval foo {
proc spong {} {
# Variable in this namespace
variable bar
puts "bar is $bar"
# Variable in another namespace
variable ::someNS::someAry
parray someAry
}
}
see how the second one reaches all the way out to ::someNS::someAry? that's the qualified name doing its thing. slay. π
π SEE ALSO π
π KEYWORDS π
global, namespace, procedure, variable
okay that's the whole page bestie!! this one's from Tcl 8.0 β variable(n), in the Tcl Built-In Commands section, dated all the way back in 8.0 era. go forth and namespace responsibly. mwah πβ¨ππ¦πΈπ«Ά
π¦ where this page comes from
- alpine 3.22: tcl-doc 8.6.16-r0 Β· TCL
- alpine 3.23: tcl-doc 8.6.17-r0 Β· TCL
- alpine 3.24: tcl-doc 8.6.17-r1 Β· TCL
- arch rolling: tcl 8.6.16-1 Β· TCL
- centos-stream 10: tcl-doc 1:8.6.13-4.el10 Β· TCL AND GPL-3.0-or-later WITH bison-exception-2.2 AND BSD-3-Clause
- centos-stream 9: tcl-doc 1:8.6.10-7.el9 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- Copyright (c) 1993-1997 Bell Labs Innovations for Lucent Technologies
- Copyright (c) 1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms