global(n)

✨ global(n) ✨

okay bestie so this is a Tcl Built-In Commands page, and it's about global β€” literally the moment your proc stops being a little isolated bubble and starts touching the real variables πŸ’…


🌸 SYNOPSIS 🌸

global ?varname ...?

πŸ’– DESCRIPTION πŸ’–

so here's the tea: this command has no effect unless you run it inside the context of a proc body. like, outside a proc? it's giving nothing. it's just vibing. no notes, no changes, no effect ✨

but if global is executed in the context of a proc body? oh she's working. she creates local variables linked to the corresponding global variables 🫢 and heads up β€” those linked variables, just like the ones made by upvar, are not included in the list that info locals returns. so don't come for them looking there, they're not showing up πŸ™ˆ

if varname contains namespace qualifiers, then the local variable's name is the unqualified name of the global variable, as determined by the namespace tail command. so the qualifiers get dropped, the tail stays, periodt.

and one more thing β€” varname is always treated as the name of a variable, never an array element. so if your name looks like an array element, like a(b), you get an error. it's giving invalid, and the interpreter will absolutely tell you πŸ’€


πŸŽ€ EXAMPLES πŸŽ€

this procedure sets the namespace variable ::a::x:

proc reset {} {
    global a::x
    set x 0
}

and this procedure accumulates the strings passed to it in a global buffer, separated by newlines 🌈 it's lowkey iconic for situations where you wanna build a message piece-by-piece (as if with puts) but then send that whole full message in a single piece β€” like over a connection opened with socket, or as part of a counted HTTP response. one message, one send, no crumbs πŸ”₯

proc accum {string} {
    global accumulator
    append accumulator $string \n
}

πŸ‘‘ SEE ALSO πŸ‘‘


⭐ KEYWORDS ⭐

global, namespace, procedure, variable


that's the whole page bestie, hope you slayed it πŸ’– this one's a Tcl built-in, filed under global(n) β€” go forth and access those globals responsibly, mwah πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from