
I wanted to make use of static variables in a script recently, that is, vars that will not get reset once the script is re-ran, etc. Basically I went and searched, discovered static vars do not exist in Tcl, and then found this code: http://phaseit.net/claird/comp.lang.tcl ... tml#static
Code: Select all
proc static {args} {
# set procName [lindex [info level [expr [info level]-1]] 0]
set procName "incith::test"
# putlog "procName is (${procName})\n"
foreach varName $args {
uplevel 1 "upvar #0 {$procName:$varName} $varName"
}
}
Code: Select all
namespace eval incith {
namespace eval test {
proc myProc {} {
static count
if {![info exists "incith::test:count"]} {
set "incith::test:count" 1
}
incr "incith::test:count"
putlog "count is now ${count}\n"
}
}
}
As you can see I modified the set procName, which I'll now explain. When I first put it into a test script, the test script was under the same namespace above, namespace eval incith { namespace eval test {, and $procName became incith::test::PROC_NAME:var, where PROC_NAME was the calling procedure, e.g. incith::test::myProc:count based on the above example. Then I went and added proc static into my XRL script and procName would not fetch the namespace it was in, but instead it would only get the procedure name, and e.g. store the var as "myProc:count", this was baffling to me, which is why I just ended up forcing set procName to my namespaces.
The questions in all of this, is this really necessary? Is there some extra work being done? The point is just to not forget variables basically, so that when the proc is re-run it knows what a certain variable it's using was since the last call, and so on. It works, at least, but I can't help but feel that it's just plain wrong. It's also dang annoying to be typing out 60 char variable names with the namespaces etc. Discuss!