Version 13 of variable

Updated 2008-03-25 18:27:40 by LV

A variable is a way of referring to data by way of a symbolic name.


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/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. 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
 }

 {} 42

will 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
 0

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.