global(3tcl)
β¨ global β Access global variables β¨
okay bestie, gather round π this is global, a Tcl built-in command, and it's literally the little fairy godmother that lets your proc reach out and touch the global scope. π§
πΈ SYNOPSIS πΈ
global ?varname ...?
short, sweet, and absolutely carrying the whole show π
π¦ DESCRIPTION π¦
Okay so real talk: this command has no effect unless it's executed in the context of a proc body. Like, if you're not inside a proc, it's giving nothing. It's just vibing. π
But the second you are inside a proc body? iconic behavior β¨ It creates local variables that are linked to the corresponding global variables. The way that works is exactly like those linked variables created by upvar β and here's the tea β those linked variables are not included in the list returned by info locals. So don't go looking for them there, they're not on that guest list. π
Now, 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 it trims the whole qualifier situation off and just uses the tail end. βοΈ
And important: varname is always treated as the name of a variable, not an array element. So if the name looks like an array element β like, say, a(b) β you get an error. Full stop. Periodt. π
π EXAMPLES π
π Example the first: setting a namespace variable
This procedure sets the namespace variable ::a::x π
proc reset {} {
global a::x
set x 0
}
see how it says global a::x but then sets x? yeah. that's the namespace tail thing doing its thing. slay. π
π Example the second: the accumulator
This procedure accumulates the strings passed to it in a global buffer, separated by newlines. π
It's useful for situations when you want to build a message piece-by-piece (as if with puts) but send that full message in a single piece β for example, over a connection opened with socket, or as part of a counted HTTP response. π¬
proc accum {string} {
global accumulator
append accumulator $string \n
}
building it up bit by bit and then dropping the whole thing at once? that's main character energy fr fr. π«Ά
π SEE ALSO π
go read those too, they're all part of the same friend group π
β KEYWORDS β
global, namespace, procedure, variable
and that's the whole vibe check, bestie. go forth and scope responsibly. π¦
this page came from the Tcl crew, documenting global(3tcl) β sending you all the love from the Tcl Built-In Commands corner of the manual. mwah. π
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms