Besides being a kind of thing in Tcl, there is also a command in tcl by the name of variable.variable - create and initialize a namespace variable http://www.purl.org/tcl/home/man/tcl/TclCmd/variable.htm
- variable ?name value...? name ?value?
You can replace global fully with variable, so procedures can be put in a namespace without need for rewriting. The drawback is that you can't specify several variables in one command without assigning values, so instead of
escargo 4 Sep 2003 - In URL behaviour in a text widget there are some code idioms I am not familiar with. One of them is:
Recently Dossy and Helmut Giese were discussing the variable command on comp.lang.tcl. Dossy replies to a remark by Helmut:
KPV 13 Sep 2003 - My biggest problem w/ using variable instead of global is in debugging. I typically debug via a console window where I past in code from the procedure I'm interested in. When I define my variables in as globals, it just works; but when the variables are buried in a namespace it doesn't--I either have to hand tweak the code to fully qualify the variable names or upvar them into the global namespace.
global foo bar grillyou'd have to write
variable foo; variable bar; variable grillBut the less globals you use, the better anyway ;-) -- RS
escargo 4 Sep 2003 - In URL behaviour in a text widget there are some code idioms I am not familiar with. One of them is:
variable {}What the heck is that supposed to do?DGP Just what the docs say. It creates a local variable named "" within the procedure as a link to the namespace variable named [namespace current]:: . Note that [namespace tail [namespace current]::] is "".In this particular case, the variable named "" is an array variable, so one can set and read its elements like so:
set (elem1) 1
set (elem2) 2
if {$(elem1) > $(elem2)} {
set greater elem1
} else {
set greater elem2
}
puts "Greater is $($greater)"escargo 5 Sep 2003 - Gosh wow. Not only is everything is a string, but nothing is a string. RS: 'Everything' includes 'nothing'...KJN 12 Aug 2004 - I came here looking for an explanation of this unusual idiom, and I'm pleased to find it!I had not realised until now that the minimum number of characters in a varName is zero! (Is that mentioned in the manual anywhere?) Even without a variable statement, it is valid to write
set {} 12
puts ${}
set {}This code will echo "12" to stdout, and return the value 12.Even more unusual: {} is acceptable as the name of a proc:
proc {} a {
puts $a
return $a
}
{} 42will echo "42" to stdout, and return the value 42.RS: For scalar variables and commands you still have to delimit the "nothing" with quotes or braces, but with array names, nothing is enough. Man Tcl says:$name(index)Name gives the name of an array variable and index gives the name of an element within that array. Name must contain only letters, digits, underscores, and namespace separators, and may be an empty string. - Hence the $(key) syntax DGP explained.Recently Dossy and Helmut Giese were discussing the variable command on comp.lang.tcl. Dossy replies to a remark by Helmut:
> So you have to distinguish between 'creating' a variable and
> 'defining' it. In your example you _create_ 'foo::bar' but don't
> _define_ it. Hence [info exist] doesn't see it - as told in the book.
Ahh, yes. Okay, so then the docs and the behavior ARE consistent, cool.
I didn't realize that a variable could be "created" but not "exist" --
weird. :-)
% namespace eval foo {
proc foo {} {
variable myarr
upvar somearr myarr
parray myarr
}
proc foo2 {} {
variable myarr
namespace which -variable myarr
}
proc foo::foo3 {} {
variable myarr
info exists myarr
}
}
% foo::foo
"myarr" isn't an array
% set somearr(x) y
y
% foo::foo
myarr(x) = y
% foo::foo2
::foo::myarr
% foo::foo3
0KPV 13 Sep 2003 - My biggest problem w/ using variable instead of global is in debugging. I typically debug via a console window where I past in code from the procedure I'm interested in. When I define my variables in as globals, it just works; but when the variables are buried in a namespace it doesn't--I either have to hand tweak the code to fully qualify the variable names or upvar them into the global namespace.
