variable(n)

πŸ“¦
version 9.0.2 Β· 3 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, centos-stream 9, fedora 43, fedora 44, opensuse-leap 16.0

πŸ’– 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 ordinary set or array commands. 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