What kinds of variables can Tcl scripts use

Purpose: discuss for the beginner the types of variables a user can use in a Tcl script.


  • [Discuss characters permitted in a variable name, format of a variable name, when to use the $ and when not, etc.]
  • Types of variables
  • In the core of Tcl
  • Scalar

Note that Tcl doesn't have much of a distinction between "scalar" and "non-scalar" things. If something is a value, it can be put into any kind of aggregation of values. The most common aggregation is a list, which is itself a value.

  1. String - an arbitrary sequence of Unicode characters. Currently (Tcl8.4) only 16-bit Unicode (the Basic Multilingual Plane) is supported, but most of the internal storage is done using UTF-8 which can encode the full 32-bit range.
  2. Numbers - a special case of the string . What kinds of numbers does Tcl recognize?
  3. Byte String - a special case of the previous type where all character codes are between 0 and 255 inclusive. Many commands acting on such strings (e.g. binary, binary file I/O) utilize an alternative storage encoding where there is one byte of memory per character in the string.
  4. List - another special case of string. In this case, the string consists of a series of elements either joined together via list, or a character string split into elements. Once a list has been constructed, individual elements can be accessed via certain Tcl commands, such as lindex, designed to expect a list as input.
  5. Handle - a symbolic string which tcl translates internally via a table into typically a pointer. An example of a handle is the value returned by open. The value that is returned is not intended to be manipulated with expr, etc. Instead, the value is important only to functions which understand the particular kind of handle. There is no type info in the handle, so one can get broken behavior by indiscriminate passing of handles to routines expecting a different type of handle.
  6. Channel - a specialized handle, used by read, puts, gets, fconfigure, etc. to refer to an open file, pipe, socket, or whatever.
  7. Regular Expression - REs can be pre-compiled by the call "regexp $RE {}" [1 ].
  8. Interpreter - a specialized handle, used to [fill in the rest]
  9. Namespace - a specialized handle, used to [fill in the rest]
  10. Thread - a specialized handle, used to [fill in the rest] [Can you access this type of variable without the seperate Thread extension?]

RT Comment: I believe this page would be better described as "What kinds of special values can Tcl variables refer to" That's because I think the generic part of the Tcl interpreter knows nothing of the special "types" referred to above. To me the most important distinctions among variables are:

  1. Is the variable a single value or the name of an array and,
  2. Is the variable global or local in scope (or for the adventurous, in a custom namespace)?

Values are a whole 'nother mater as per the list above but they can be freely copied between variables as they are just lookup codes.


[Does it make much sense for a beginner to think of namespaces and interpreters as handles?]


  • In common extensions
  • In Tk, the special variable types are specialized handles.
  1. widget - a handle representing a type of (usually visual) component for creating graphical interfaces. Some examples are frame, button, menu, text, entry, and so forth.
  2. font - this handle represents a combination of face, size, and thickness that can then be used when specifying to a widget how to display text
  3. image - this handle represents an image

  • In TclX
  1. keyed list - this specialized version of a list permits one to create pairs of key/value elements for the list and then retrieve values by specifying the key desired. It allows representing data a bit more structured. Keyed lists are intended to provide array like functionality but in a list format,

  • In BLT
  1. vector - specialized data type to handle large amounts of numeric data rapidly

  1. Relation -- implements a relation data type with a complete algebra of operations.
   [[Add other extensions which create new types of variables]]