Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variable in Tcl

Tags:

static

tcl

Is it possible to declare a static variable in Tcl?
I use a certain function to catch unknown command errors, and I want it to print an error message on the first appearance of an unknown command - so I need to keep something like a static list inside the proc. Is that possible?

like image 445
Amir Rachum Avatar asked May 25 '11 10:05

Amir Rachum


Video Answer


2 Answers

Or you can just use a straight global variable:

set varList {}

proc useCount {value} {
    global varList ;
    lappend varList $value
}

useCount One
useCount Two
puts $varList
like image 69
Jackson Avatar answered Nov 13 '22 13:11

Jackson


No. But you can use a global (usually namespaced) array indexed by proc name for instance:

namespace eval foo {
  variable statics
  array set statics {}
}
...
proc ::foo::bar args {
  variable statics
  upvar 0 statics([lindex [info level 0] 0]) myvar
  # use myvar
}
like image 37
kostix Avatar answered Nov 13 '22 15:11

kostix