Version 7 of variable

Updated 2003-09-05 16:52:02

variable - create and initialize a namespace variable http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/variable.htm

 variable ?name value...? name ?value?  

This command is normally used within a namespace eval command to create one or more variables within a namespace. Each variable name is initialized with value. The value for the last variable is optional. If a variable name does not exist, it is created. In this case, if value is specified, it is assigned to the newly created variable. If no value is specified, the new variable is left undefined. If the variable already exists, it is set to value if value is specified or left unchanged if no value is given. Normally, name is unqualified (does not include the names of any containing namespaces), and the variable is created in the current namespace. If name includes any namespace qualifiers, the variable is created in the specified namespace.

If the variable command is executed inside a Tcl procedure, it creates local variables linked to the corresponding namespace variables. In this way the variable command resembles the global command, although the global command only links to variables in the global namespace. If any values are given, they are used to modify the values of the associated namespace variables. If a namespace variable does not exist, it is created and optionally initialized.

A name argument cannot reference an element within an array. Instead, name should reference the entire array, and the initialization value should be left off. After the variable has been declared, elements within the array can be set using ordinary set or array commands. (From: Tcl Help)


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

 global foo bar grill

you'd have to write

 variable foo; variable bar; variable grill

But 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.


Recently Dossy amd 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
 0

Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command from Tcl