Updated 2012-12-01 04:39:49 by RLE

info vars ?pattern?

If pattern isn't specified, returns a list of all the names of currently-visible variables. This includes locals and currently-visible globals. If pattern is specified, only those names matching pattern are returned. The matching of pattern is determined using the same rules as for string match. Pattern can be a qualified name like Foo::option*. That is, it may specify a particular namespace using a sequence of namespace names separated by ::s, and may have pattern matching special characters at the end to specify a set of variables in that namespace. If pattern is a qualified name, the resulting list of variable names has each matching namespace variable qualified with the name of its namespace.

Question: in what cases do these two info commands differ?
  set a 1

  set b [info exists a]

and
  set c [info vars a]

The doc for info exists indicates that the variable exists and has a value in either the current or global space.

Certainly, one potential problem is if the variable has any pattern characters in it.

Are there other differences between the two?

MG May 18th - [info exists varName] will return 1 if a variable named varName exists, and 0 if it doesn't. [info vars varName] returns a list of all the variables which match the pattern varName. For instance...
 1 % set var1 bleh
 bleh
 2 % set var2 bleh
 bleh
 3 % set var3 bleh
 bleh
 4 % info exists var1
 1
 5 % info exists var4
 0
 6 % info vars var1
 var1
 7 % info vars var4
 8 % info exists var*
 0
 9 % info vars var*
 var1 var2 var3

CHL July 18th - There is a difference when the variable exists and has no value. In that case [info exist varName] will return 0 whereas [info vars varName] will return a list containing varName. For instance...
 1 % namespace eval nspace { variable novalue }
 2 % info exists nspace::novalue
 0
 3 % info vars nspace::novalue
 nspace::novalue

See also: