global - Access global variables http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/global.htm global varname ?varname ...? This command is ignored unless a Tcl procedure is being interpreted. If so then it declares the given varname's to be global variables rather than local ones. Global variables are variables in the global namespace. For the duration of the current procedure (and only while executing in the current procedure), any reference to any of the varnames will refer to the global variable by the same name. (from: Tcl Help) ---- Find out which globals you have with '''[[info globals]'''. ---- Globals are expensive if you call them only a few times in the proc - the cost of the initial linking to a local variable weighs heavily; they are quite fast if you use them a lot (the breakeven seems to be around 15 reads ...). For fewer calls, use fully qualified variable names ''(::name)'' instead. (from: [Can you run this benchmark 10 times faster]) ---- '''Globbing globals''': Want to import several globals in one go, with glob wildcards (similar to the ''public'' statement in VB)? This comes from David Cuthbert (mailto:dacut@kanga.org): proc globalpat {args} { foreach pattern $args { set varnames [info globals $pattern] if {[llength $varnames] != 0} { uplevel 1 global $varnames } } } To use: proc hello {} { globalpat *tcl* puts $tcl_patchLevel } % hello 8.2.2 ---- Using the [variable] command in place of [[global]] is often good practice, even when the variable ''is'' global. If you later decide that you want to encapsulate a group of globals in a namespace, you can do so easily. If you have something like: proc myProc {} { global myvar1 global myvar2 # ... } you have to go off and edit a bunch of code if you later decide that '''myProc''' and its associated variables should be in a namespace. If instead you say: proc myProc {} { variable myvar1 variable myvar2 # ... } the variables are still global, but the procedure becomes easy to encapsulate: just wrap it in [[namespace eval]]: namespace eval myNamespace { proc myProc {} { variable myvar1 variable myvar2 # ... } } and now '''myvar1''' and '''myvar2''' are namespace variables. In general, you should use [[global]] only when you have a particular reason to make a variable global -- such as making it the text variable of a widget. Even then, consider something like: label .path.to.label -textvariable [namespace current]::varName ---- [LV] July 7, 2003 In the above discussion, we see [variable] recommended over global. Is ''variable'' better to use than the '::name'' notation as well? [DGP] July 7, 2003 For the purpose we are discussing, yes. If you want to keep a body of code free to move from one namespace to another, you should use relative variable names, and make use of [[variable]] and [[namespace which -variable]] to set up scope and generate fully qualified names as needed. ---- [Tcl syntax help] - [Arts and crafts of Tcl-Tk programming] - [Category Command] from [Tcl]