I usually refer to code outside of any procedures as being in the global scope. (Unless it's inside of a namespace block, in which case I cringe a bit and call it the namespace scope because I don't know what else to call it.) I'm not a formal computer linguist, so I don't get too hung up on the terms I'm using as long as everyone knows what I'm talking about.
As an aside for those people who might not be aware of it, it's legal for a Tcl procedure to create a new global variable. For example:
Code:
proc init {} {
global state
set state "disconnected"
}
# ...
init
This is particularly handy for libraries of Tcl code, when you need to initialize the state of the library code before executing other library procedures. Also, I know that some programmers who come from a C/C++ background like to create a
main procedure in their Tcl code, and then explicitly call
main to start their application running.
This can actually provide a slight performance advantage, especially if you've got one or more loops that will run many iterations. Tcl procedures are bytecode-compiled, and thus they can run faster than global, uncompiled code. But it doesn't make much difference unless there's a lot of code to run, like the long loops I mentioned above:
Code:
% time {
for {set i 0} {$i < 100000} {incr i} {
lappend list1 [expr { rand()*20000 - 10000 }]
}
}
310138 microseconds per iteration
% proc init {} {
global list2
for {set i 0} {$i < 100000} {incr i} {
lappend list2 [expr { rand()*20000 - 10000 }]
}
}
% time { init }
165162 microseconds per iteration
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting,
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax