# Tcl Programming/Print version
## Tcl: the Tool Command language
### Introduction
#### So what is Tcl?

The name Tcl is derived from "Tool Command Language" and is pronounced "tickle". Tcl is a radically simple open-source interpreted programming language that provides common facilities such as variables, procedures, and control structures as well as many useful features that are not found in any other major language. Tcl runs on almost all modern operating systems such as Unix, Macintosh, and Windows (including Windows Mobile).
While Tcl is flexible enough to be used in almost any application imaginable, it does excel in a few key areas, including: automated interaction with external programs, embedding as a library into application programs, language design, and general scripting.
Tcl was created in 1988 by John Ousterhout and is distributed under a BSD style license (which allows you everything GPL does, plus closing your source code). The current stable version, in February 2008, is 8.5.1 (8.4.18 in the older 8.4 branch).
The first major GUI extension that works with Tcl is Tk, a toolkit that aims to rapid GUI development. That is why Tcl is now more commonly called Tcl/Tk.
The language features far-reaching introspection, and the syntax, while simple, is very different from the Fortran/Algol/C++/Java world. Although Tcl is a string based language there are quite a few object-oriented extensions for it like Snit, incr Tcl, and XOTcl to name a few.
Tcl was originally developed as a reusable command language for experimental computer aided design (CAD) tools. The interpreter is implemented as a C library that could be linked into any application. It is very easy to add new functions to the Tcl interpreter, so it is an ideal reusable "macro language" that can be integrated into many applications.
However, Tcl is a programming language in its own right, which can be roughly described as a cross-breed between
* LISP/Scheme (mainly for its tail-recursion capabilities),
* C (control structure keywords, expr syntax) and
* Unix shells (but with more powerful structuring).

#### One language, many styles

Although a language where "everything is a command" appears like it must be "imperative" and "procedural", the flexibility of Tcl allows one to use functional or object-oriented styles of programming very easily. See "Tcl examples" below for ideas what one can do.
The traditional, "procedural" approach would be
Here is yet another style (not very fast on long lists, but depends on nothing but Tcl). It works by building up an expression, where the elements of the lists are joined with a plus sign, and then evaluating that:
From Tcl 8.5, with math operators exposed as commands, and the expand operator, this style is better:
or, if you have imported the tcl::mathop operators, just
Note that all of the above are valid stand alone Tcl scripts.
It is also very easy to implement other programming languages (be they (reverse) polish notation, or whatever) in Tcl for experimenting. One might call Tcl a "CS Lab". For instance, here's how to compute the average of a list of numbers in Tcl (after first writing somewhat more Tcl to implement a J-like functional language - see Tacit programming in examples):
or, one could implement a RPN language similar to FORTH or Postscript and write:
A more practical aspect is that Tcl is very open for "language-oriented programming" - when solving a problem, specify a (little) language which most simply describes and solves that problem - then go implement that language...

#### Why should I use Tcl?

Good question. The general recommendation is: "Use the best tool for the job". A good craftsman has a good set of tools, and knows how to use them best.
Tcl is a competitor to other scripting languages like awk, Perl, Python, PHP, Visual Basic, Lua, Ruby, and whatever else will come along. Each of these has strengths and weaknesses, and when some are similar in suitability, it finally becomes a matter of taste.
Points in favour of Tcl are:
* simplest syntax (which can be easily extended)
* cross-platform availability: Mac, Unix, Windows, ...
* strong internationalization support: everything is a Unicode string
* robust, well-tested code base
* the Tk GUI toolkit speaks Tcl natively
* BSD license, which allows open-source use like GPL, as well as closed-source
* a very helpful community, reachable via newsgroup, Wiki, or chat :)
Tcl is not the best solution for every problem. It is however a valuable experience to find out what is possible with Tcl.

#### Example: a tiny web server

Before spoon-feeding the bits and pieces of Tcl, a slightly longer example might be appropriate, just so you get the feeling how it looks. The following is, in 41 lines of code, a complete little web server that serves static content (HTML pages, images), but also provides a subset of CGI functionality: if an URL ends with .tcl, a Tcl interpreter is called with it, and the results (a dynamically generated HTML page) served.
Note that no extension package was needed - Tcl can, with the socket command, do such tasks already pretty nicely. A socket is a channel that can be written to with puts. The fcopy copies asynchronously (in the background) from one channel to another, where the source is either a process pipe (the "exec tclsh" part) or an open file.
This server was tested to work pretty well even on 200MHz Windows 95 over a 56k modem, and serving several clients concurrently. Also, because of the brevity of the code, this is an educational example for how (part of) HTTP works.
And here's a little "CGI" script I tested it with (save as time.tcl):

#### Where to get Tcl/Tk

On most Linux systems, Tcl/Tk is already installed. You can find out by typing tclsh at a console prompt (xterm or such). If a "%" prompt appears, you're already set. Just to make sure, type info pa at the % prompt to see the patchlevel (e.g. 8.4.9) and info na to see where the executable is located in the file system.
Tcl is an open source project. The sources are available from http://tcl.sourceforge.net/ if you want to build it yourself.
For all major platforms, you can download a binary ActiveTcl distribution from ActiveState. Besides Tcl and Tk, this also contains many popular extensions - it's called the canonical "Batteries Included" distribution.
Alternatively, you can get Tclkit: a Tcl/Tk installation wrapped in a single file, which you don't need to unwrap. When run, the file mounts itself as a virtual file system, allowing access to all its parts.
January 2006, saw the release of a new and promising one-file vfs distribution of Tcl; eTcl. Free binaries for Linux, Windows, and Windows Mobile 2003 can be downloaded from http://www.evolane.com/software/etcl/index.html . Especially on PocketPCs, this provides several features that have so far been missing from other ports: sockets, window "retreat", and can be extended by providing a startup script, and by installing pure-Tcl libraries.

#### First steps

To see whether your installation works, you might save the following text to a file hello.tcl and run it (type tclsh hello.tcl at a console on Linux, double-click on Windows):
It should bring up a little grey window with the greeting.
To make a script directly executable (on Unix/Linux, and Cygwin on Windows), use this first line (the # being flush left):
or (in an older, deprecated tricky style):
This way, the shell can determine which executable to run the script with.
An even simpler way, and highly recommended for beginners as well as experienced users, is to start up tclsh or wish interactively. You will see a % prompt in the console, can type commands to it, and watch its responses. Even error messages are very helpful here, and don't cause a program abort - don't be afraid to try whatever you like! Example:
You can even write programs interactively, best as one-liners:
For more examples, see the chapter "A quick tour".

### Syntax

Syntax is just the rules how a language is structured. A simple syntax of English could say (ignoring punctuation for the moment):
* A text consists of one or more sentences
* A sentence consists of one or more words
Simple as this is, it also describes Tcl's syntax very well - if you say "script" for "text", and "command" for "sentence". There's also the difference that a Tcl word can again contain a script or a command. So
is a command consisting of three words: if, a condition in braces, a command (also consisting of three words) in braces.
is a well-formed Tcl command: it calls Take (which must have been defined before) with the three arguments "this", "for", and "example". It is up to the command how it interprets its arguments, e.g.
will write the string "acos(-1)" to the stdout channel, and return the empty string "", while
will compute the arc cosine of -1 and return 3.14159265359 (an approximation of Pi), or
will invoke the string command, which again dispatches to its length sub-command, which determines the length of the second argument and returns 8.

#### Quick summary

A Tcl script is a string that is a sequence of commands, separated by newlines or semicolons.
A command is a string that is a list of words, separated by blanks. The first word is the name of the command, the other words are passed to it as its arguments. In Tcl, "everything is a command" - even what in other languages would be called declaration, definition, or control structure. A command can interpret its arguments in any way it wants - in particular, it can implement a different language, like expr.
A word is a string that is a simple word, or one that begins with { and ends with the matching } (braces), or one that begins with " and ends with the matching ". Braced words are not evaluated by the parser. In quoted words, substitutions can occur before the command is called:
* $[A-Za-z0-9_]+ substitutes the value of the given variable. Or, if the variable name contains characters outside that regular expression, another layer of bracing helps the parser to get it right:
If the code would say $Schüler, this would be parsed as the value of variable $Sch, immediately followed by the constant string üler.
* (Part of) a word can be an embedded script: a string in brackets whose contents are evaluated as a script (see above) before the current command is called.
In short: Scripts and commands contain words. Words can again contain scripts and commands. (This can lead to words more than a page long...)
Arithmetic and logic expressions are not part of the Tcl language itself, but the language of the expr command (also used in some arguments of the if, for, while commands) is basically equivalent to C's expressions, with infix operators and functions. See separate chapter on expr below.

#### The man page: 11 rules

Here is the complete manpage for Tcl (8.4) with the "endekalogue", the 11 rules. (From 8.5 onward there is a twelfth rule regarding the {*} feature).
The following rules define the syntax and semantics of the Tcl language:
(1) Commands A Tcl script is a string containing one or more commands. Semi-colons and newlines are command separators unless quoted as described below. Close brackets are command terminators during command substitution (see below) unless quoted.
(2) Evaluation A command is evaluated in two steps. First, the Tcl interpreter breaks the command into words and performs substitutions as described below. These substitutions are performed in the same way for all commands. The first word is used to locate a command procedure to carry out the command, then all of the words of the command are passed to the command procedure. The command procedure is free to interpret each of its words in any way it likes, such as an integer, variable name, list, or Tcl script. Different commands interpret their words differently.
(3) Words Words of a command are separated by white space (except for newlines, which are command separators).
(4) Double quotes If the first character of a word is double-quote (") then the word is terminated by the next double-quote character. If semi-colons, close brackets, or white space characters (including newlines) appear between the quotes then they are treated as ordinary characters and included in the word. Command substitution, variable substitution, and backslash substitution are performed on the characters between the quotes as described below. The double-quotes are not retained as part of the word.
(5) Braces If the first character of a word is an open brace ( then the word is terminated by the matching close brace). Braces nest within the word: for each additional open brace there must be an additional close brace (however, if an open brace or close brace within the word is quoted with a backslash then it is not counted in locating the matching close brace). No substitutions are performed on the characters between the braces except for backslash-newline substitutions described below, nor do semi-colons, newlines, close brackets, or white space receive any special interpretation. The word will consist of exactly the characters between the outer braces, not including the braces themselves.
(6) Command substitution If a word contains an open bracket ( then Tcl performs command substitution. To do this it invokes the Tcl interpreter recursively to process the characters following the open bracket as a Tcl script. The script may contain any number of commands and must be terminated by a close bracket). The result of the script (i.e. the result of its last command) is substituted into the word in place of the brackets and all of the characters between them. There may be any number of command substitutions in a single word. Command substitution is not performed on words enclosed in braces.
(7) Variable substitution If a word contains a dollar-sign ($) then Tcl performs variable substitution: the dollar-sign and the following characters are replaced in the word by the value of a variable. Variable substitution may take any of the following forms:
$name
Name is the name of a scalar variable; the name is a sequence of one or more characters that are a letter, digit, underscore, or namespace separators (two or more colons).
$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. Command substitutions, variable substitutions, and backslash substitutions are performed on the characters of index.
${name}
Name is the name of a scalar variable. It may contain any characters whatsoever except for close braces. There may be any number of variable substitutions in a single word. Variable substitution is not performed on words enclosed in braces.
(8) Backslash substitution If a backslash (\) appears within a word then backslash substitution occurs. In all cases but those described below the backslash is dropped and the following character is treated as an ordinary character and included in the word. This allows characters such as double quotes, close brackets, and dollar signs to be included in words without triggering special processing. The following table lists the backslash sequences that are handled specially, along with the value that replaces each sequence.

##### \a

    Audible alert (bell) (0x7).

##### \b

    Backspace (0x8).

##### \f

    Form feed (0xc).

##### \n

    Newline (0xa).

##### \r

    Carriage-return (0xd).

##### \t

    Tab (0x9).

##### \v

    Vertical tab (0xb).

##### \<newline>whiteSpace

    A single space character replaces the backslash, newline, and all spaces and tabs after the newline. This backslash sequence is unique in that it is replaced in a separate pre-pass before the command is actually parsed. This means that it will be replaced even when it occurs between braces, and the resulting space will be treated as a word separator if it isn't in braces or quotes.
    Literal backslash (\), no special effect.

##### \ooo

    The digits ooo (one, two, or three of them) give an eight-bit octal value for the Unicode character that will be inserted. The upper bits of the Unicode character will be 0.

##### \xhh

    The hexadecimal digits hh give an eight-bit hexadecimal value for the Unicode character that will be inserted. Any number of hexadecimal digits may be present; however, all but the last two are ignored (the result is always a one-byte quantity). The upper bits of the Unicode character will be 0.

##### \uhhhh

    The hexadecimal digits hhhh (one, two, three, or four of them) give a sixteen-bit hexadecimal value for the Unicode character that will be inserted.
Backslash substitution is not performed on words enclosed in braces, except for backslash-newline as described above.
(9) Comments If a hash character (#) appears at a point where Tcl is expecting the first character of the first word of a command, then the hash character and the characters that follow it, up through the next newline, are treated as a comment and ignored. The comment character only has significance when it appears at the beginning of a command.
(10) Order of substitution Each character is processed exactly once by the Tcl interpreter as part of creating the words of a command. For example, if variable substitution occurs then no further substitutions are performed on the value of the variable; the value is inserted into the word verbatim. If command substitution occurs then the nested command is processed entirely by the recursive call to the Tcl interpreter; no substitutions are performed before making the recursive call and no additional substitutions are performed on the result of the nested script. Substitutions take place from left to right, and each substitution is evaluated completely before attempting to evaluate the next. Thus, a sequence like
will always set the variable y to the value, 012.
(11) Substitution and word boundaries Substitutions do not affect the word boundaries of a command. For example, during variable substitution the entire value of the variable becomes part of a single word, even if the variable's value contains spaces.

#### Comments

The first rule for comments is simple: comments start with # where the first word of a command is expected, and continue to the end of line (which can be extended, by a trailing backslash, to the following line):
One of the problems new users of Tcl meet sooner or later is that comments behave in an unexpected way. For example, if you comment out part of code like this:
This happens to work, but any unbalanced braces in comments may lead to unexpected syntax errors. The reason is that Tcl's grouping (determining word boundaries) happens before the # characters are considered.
To add a comment behind a command on the same line, just add a semicolon:
Comments are only taken as such where a command is expected. In data (like the comparison values in switch), a # is just a literal character:
To comment out multiple lines of code, it is easiest to use "if 0":

### Data types

In Tcl, all values are strings, and the phrase "Everything is a string" is often used to illustrate this fact. But just as 2 can be interpreted in English as "the number 2" or "the character representing the number 2", two different functions in Tcl can interpret the same value in two different ways. The command expr, for example, interprets "2" as a number, but the command string length interprets "2" as a single character. All values in Tcl can be interpreted either as characters or something else that the characters represent. The important thing to remember is that every value in Tcl is a string of characters, and each string of characters might be interpreted as something else, depending on the context. This will become more clear in the examples below. For performance reasons, versions of Tcl since 8.0 keep track of both the string value and how that string value was last interpreted. This section covers the various "types" of things that Tcl values (strings) get interpreted as.

#### Strings

A string is a sequence of zero or more characters (where all 16-bit Unicodes are accepted in almost all situations, see in more detail below). The size of strings is automatically administered, so you only have to worry about that if the string length exceeds the virtual memory size.
In contrast to many other languages, strings in Tcl don't need quotes for markup. The following is perfectly valid:
Quotes (or braces) are rather used for grouping:
The difference is that inside quotes, substitutions (like of variables, embedded commands, or backslashes) are performed, while in braces, they are not (similar to single quotes in shells, but nestable):
In source code, quoted or braced strings can span multiple lines, and the physical newlines are part of the string too:
To reverse a string, we let an index i first point at its end, and decrementing i until it's zero, append the indexed character to the end of the result res:
    amanaP - lanac a ,nalp a ,nam A
Hex-dumping a string:
    68 65 6c 6c 6f
Finding a substring in a string can be done in various ways:
The matching is done with exact match in string first, with glob-style match in string match, and as a regular expression in regexp. If there are characters in substr that are special to glob or regular expressions, using string first is recommended.

#### Lists

Many strings are also well-formed lists. Every simple word is a list of length one, and elements of longer lists are separated by whitespace. For instance, a string that corresponds to a list of three elements:
Strings with unbalanced quotes or braces, or non-space characters directly following closing braces, cannot be parsed as lists directly. You can explicitly split them to make a list.
The "constructor" for lists is of course called list. It's recommended to use when elements come from variable or command substitution (braces won't do that). As Tcl commands are lists anyway, the following is a full substitute for the list command:
Lists can contain lists again, to any depth, which makes modelling of matrixes and trees easy. Here's a string that represents a 4 x 4 unit matrix as a list of lists. The outer braces group the entire thing into one string, which includes the literal inner braces and whitespace, including the literal newlines. The list parser then interprets the inner braces as delimiting nested lists.
The newlines are valid list element separators, too.
Tcl's list operations are demonstrated in some examples:
Note that only lappend, above is mutating. To change an element of a list (of a list...) in place, the lset command is useful - just give as many indexes as needed:
    {a b} {c d}
    {a b} {c x}
The lindex command also takes multiple indexes:
    x
Example: To find out whether an element is contained in a list (from Tcl 8.5, there's the in operator for that):
    1
    #ignore this line, which is only here because there is currently a bug in wikibooks rendering which makes the 0 on the following line disappear when it is alone 0
Example: remove an element from a list variable by value (converse to lappend), if present:
    foo bar grill
    foo grill
    foo grill
A simpler alternative, which also removes all occurrences of el:
Example: To draw a random element from a list L, we first determine its length (using llength), multiply that with a random number > 0.0 and < 1.0, truncate that to integer (so it lies between 0 and length-1), and use that for indexing (lindex) into the list:
Example: Transposing a matrix (swapping rows and columns), using integers as generated variable names:
    {1 3 5} {2 4 6}
Example: pretty-printing a list of lists which represents a table:
Testing:
    +---+------------------+--------------------+ | 1 | short | long field content | | 2 | another long one | short | | 3 | | hello | +---+------------------+--------------------+
Enumerations: Lists can also be used to implement enumerations (mappings from symbols to non-negative integers). Example for a nice wrapper around lsearch/lindex:
This assigns "apple" to 0, "blueberry" to 1, etc.
    3
    cherry

#### Numbers

Numbers are strings that can be parsed as such. Tcl supports integers (32-bit or even 64-bit wide) and "double" floating-point numbers. From Tcl 8.5 on, bignums (integers of arbitrarily large precision) are supported. Arithmetics is done with the expr command, which takes basically the same syntax of operators (including ternary x?y:z), parens, and math functions as C. See below for detailed discussion of expr.
Control the display format of numbers with the format command which does appropriate rounding:
    0.666666666667
    0.67
Up to the 8.4 version (the present version is 8.5), Tcl honored the C convention that an integer starting with 0 is parsed as octal, so
This changes in 8.5, though - too often people stumbled over "08" meant as hour or month, raised a syntax error, because 8 is no valid octal number. In the future you'd have to write 0o377 if you really mean octal. You can do number base conversions with the format command, where the format is %x for hex, %d for decimal, %o for octal, and the input number should have the C-like markup to indicate its base:
    ff
    255
    377
       255
Variables with integer value can be most efficiently modified with the incr command:
The maximal positive integer can be determined from the hexadecimal form, with a 7 in front, followed by several "F" characters. Tcl 8.4 can use "wide integers" of 64 bits, and the maximum integer there is
    9223372036854775807
Demonstration: one more, and it turns into the minimum integer:
    -9223372036854775808
Bignums: from Tcl 8.5, integers can be of arbitrary size, so there is no maximum integer anymore. Say, you want a big factorial:
    93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
IEEE special floating-point values: Also from 8.5, Tcl supports a few special values for floating-point numbers, namely Inf (infinity) and NaN (Not a Number):
    Inf
    Inf
    1
    NaN
    #ignore this line, which is only here because there is currently a bug in wikibooks rendering which makes the 0 on the following line disappear when it is alone 0

#### Booleans

Tcl supports booleans as numbers in a similar fashion to C, with 0 being false and any other number being true. It also supports them as the strings "true", "false", "yes" and "no" and few others (see below). The canonical "true" value (as returned by Boolean expressions) is 1.
    0 -> 0 1 -> 1 2 -> 1 13 -> 1 true -> 1 false -> 0 on -> 1 off -> 0 no -> 0 yes -> 1 n -> 0 y -> 1 expected boolean value but got "a"

#### Characters

Characters are abstractions of writing elements (e.g. letters, digits, punctuation characters, Chinese ideographs, ligatures...). In Tcl since 8.1, characters are internally represented with Unicode, which can be seen as unsigned integers between 0 and 65535 (recent Unicode versions have even crossed that boundary, but the Tcl implementation currently uses a maximum of 16 bits). Any Unicode U+XXXX can be specified as a character constant with an \uXXXX escape. It is recommended to only use ASCII characters (\u0000-\u007f) in Tcl scripts directly, and escape all others.
Convert between numeric Unicode and characters with
Watch out that int values above 65535 produce 'decreasing' characters again, while negative int even produces two bogus characters. format does not warn, so better test before calling it.
Sequences of characters are called strings (see above). There is no special data type for a single character, so a single character is just a string on length 1 (everything is a string). In UTF-8, which is used internally by Tcl, the encoding of a single character may take from one to three bytes of space. To determine the bytelength of a single character:
String routines can be applied to single characters too, e.g [string toupper] etc. Find out whether a character is in a given set (a character string) with
As Unicodes for characters fall in distinct ranges, checking whether a character's code lies within a range allows a more-or-less rough classification of its category:
This is a useful helper to convert all characters beyond the ASCII set to their \u.... escapes (so the resulting string is strict ASCII):

#### Internal representation

In the main Tcl implementation, which is written in C, each value has both a string representation (UTF-8 encoded) and a structured representation. This is an implementation detail which allows for better performance, but has no semantic impact on the language. Tcl tracks both representations, making sure that if one is changed, the other one is updated to reflect the change the next time it is used. For example, if the string representation of a value is "8", and the value was last used as a number in an [expr] command, the structured representation will be a numeric type like a signed integer or a double-precision floating point number. If the value "one two three" was last used in one of the list commands, the structured representation will be a list structure. There are various other "types" on the C side which may be used as the structured representation. As of Tcl 8.5, only the most recent structured representation of a value is stored, and it is replaced with a different representation when necessary. This "dual-porting" of values helps avoid, repeated parsing or "stringification", which otherwise would happen often because each time a value is encountered in source code, it is interpreted as a string prior to being interpreted in its current context. But to the programmer, the view that "everything is a string" is still maintained.
These values are stored in reference-counted structures termed objects (a term that has many meanings). From the perspective of all code that uses values (as opposed to code implementing a particular representation), they are immutable. In practice, this is implemented using a copy-on-write strategy.

### Variables

Variables can be local or global, and scalar or array. Their names can be any string not containing a colon (which is reserved for use in namespace separators) but for the convenience of $-dereference one usually uses names of the pattern [A-Za-z0-9_]+, i.e. one or more letters, digits, or underscores.
Variables need not be declared beforehand. They are created when first assigned a value, if they did not exist before, and can be unset when no longer needed:
Retrieving a variable's value with the $foo notation is only syntactic sugar for [set foo]. The latter is more powerful though, as it can be nested, for deeper dereferencing:
Some people might expect $$$grill to deliver the same result, but it doesn't, because of the Tcl parser. When it encounters the first and second $ sign, it tries to find a variable name (consisting of one or more letters, digits, or underscores) in vain, so these $ signs are left literally as they are. The third $ allows substitution of the variable grill, but no backtracking to the previous $'s takes place. So the evaluation result of $$$grill is $$bar. Nested [set] commands give the user more control.

#### Local vs. global

A local variable exists only in the procedure where it is defined, and is freed as soon as the procedure finishes. By default, all variables used in a proc are local.
Global variables exist outside of procedures, as long as they are not explicitly unset. They may be needed for long-living data, or implicit communication between different procedures, but in general it's safer and more efficient to use globals as sparingly as possible. Example of a very simple bank with only one account:
This illustrates two ways of referring to global variables - either with the global command, or by qualifying the variable name with the :: prefix. The variable amount is local in both procedures, and its value is that of the first argument to the respective procedure.
Introspection:
To make all global variables visible in a procedure (not recommended):

#### Scalar vs. array

All of the value types discussed above in Data types can be put into a scalar variable, which is the normal kind.
Arrays are collections of variables, indexed by a key that can be any string, and in fact implemented as hash tables. What other languages call "arrays" (vectors of values indexed by an integer), would in Tcl rather be lists. Some illustrations:
A fanciful array name is "" (the empty string, therefore we might call this the "anonymous array" :) which makes nice reading:
Note that arrays themselves are not values. They can be passed in and out of procedures not as $capital (which would try to retrieve the value), but by reference. The dict type (available from Tcl 8.5) might be better suited for these purposes, while otherwise providing hash table functionality, too.

#### System variables

At startup, tclsh provides the following global variables:

##### argc

    number of arguments on the command line

##### argv

    list of the arguments on the command line

##### argv0

    name of the executable or script (first word on command line)

##### auto_index

    array with instructions from where to load further commands

##### auto_oldpath

    (same as auto_path ?)

##### auto_path

    list of paths to search for packages

##### env

    array, mirrors the environment variables

##### errorCode

    type of the last error, or, e.g. ARITH DIVZERO {divide by zero}

##### errorInfo

    last error message, or

##### tcl_interactive

    1 if interpreter is interactive, else 0

##### tcl_libPath

    list of library paths

##### tcl_library

    path of the Tcl system library directory

##### tcl_patchLevel

    detailed version number, e.g. 8.4.11

##### tcl_platform

    array with information on the operating system

##### tcl_rcFileName

    name of the initial resource file

##### tcl_version

    brief version number, e.g. 8.4
One can use temporary environment variables to control a Tcl script from the command line, at least in Unixoid systems including Cygwin. Example scriptlet:
This script will typically report
    foo=42
To remote-control it without editing, set the DO variable before the call:
which will evidently report
    foo=4711

#### Dereferencing variables

A reference is something that refers, or points, to another something (if you pardon the scientific expression). In C, references are done with *pointers* (memory addresses); in Tcl, references are strings (everything is a string), namely names of variables, which via a hash table can be resolved (dereferenced) to the "other something" they point to:
This can be done more than one time with nested set commands. Compare the following C and Tcl programs, that do the same (trivial) job, and exhibit remarkable similarity:
...and Tcl:
The asterisks in C correspond to calls to set in Tcl dereferencing. There is no corresponding operator to the C & because, in Tcl, special markup is not needed in declaring references. The correspondence is not perfect; there are four set calls and only three asterisks. This is because mentioning a variable in C is an implicit dereference. In this case, the dereference is used to pass its value into printf. Tcl makes all four dereferences explicit (thus, if you only had 3 set calls, you'd see hello, i). A single dereference is used so frequently that it is typically abbreviated with $varname, e.g.
has set where C uses asterisks, and $ for the last (default) dereference.
The hashtable for variable names is either global, for code evaluated in that scope, or local to a proc. You can still "import" references to variables in scopes that are "higher" in the call stack, with the upvar and global commands. (The latter being automatic in C if the names are unique. If there are identical names in C, the innermost scope wins).

#### Variable traces

One special feature of Tcl is that you can associate traces with variables (scalars, arrays, or array elements) that are evaluated optionally when the variable is read, written to, or unset.
Debugging is one obvious use for that. But there are more possibilities. For instance, you can introduce constants where any attempt to change their value raises an error:
    can't set "x": constant
The trace callback gets three words appended: the name of the variable; the array key (if the variable is an array, else ""), and the mode:
* r - read
* w - write
* u - unset
If the trace is just a single command like above, and you don't want to handle these three, use a comment ";#" to shield them off.
Another possibility is tying local objects (or procs) with a variable - if the variable is unset, the object/proc is destroyed/renamed away.

## Commands and Functions
### Commands

Commands are basically divided into C-defined ones, procedures, and aliases (and, from Tcl 8.5 onwards, ensembles). You can rename any command with
To delete a command (make it no more reachable), use the empty string as new name:
Introspection: Get a list of all defined commands with

#### C-defined commands

These are implemented in C and registered to be available in the Tcl interpreter. They can come from the Tcl core, or a loaded shared library (DLL) - for instance Tk.
To get a list of built-in commands, subtract the result of info procs from that of info commands:
The following C-defined commands are available in a fresh tclsh. For detailed documentation, see the respective man pages, e.g. at http://www.tcl.tk/man/tcl8.5/TclCmd/ - I will characterize each only very briefly:

##### after

    group of commands for timed events

##### after msec ?script?

    waits, or executes the script after, some time

##### append varName arg..

    appends the arguments to a string variable

##### array

    group of commands for arrays

##### binary

    group of commands for binary scanning and formatting

##### break

    terminate current loop

##### case

    deprecated, use switch

##### catch script ?varName?

    catch possible error in script

##### cd path

    change working directory

##### clock

    group of commands dealing with date and time

##### close handle

    closes a channel (file, socket, etc.)

##### concat list..

    make one space-separated list of the arguments

##### continue

    start next turn of current loop

##### encoding

    group of commands dealing with character set encoding

##### eof handle

    1 if channel is at end of file, else 0

##### error message ?info? ?code?

    raise an error with the given message

##### eval arg..

    evaluate the arguments as script

##### exec file arg..

    execute a separate process

##### exit ?int?

    terminate this process, return status 0..127

##### expr arg..

    arithmetic and logic engine, using C-like syntax and functions (variables referenced with $name). In addition, from Tcl 8.4 there are eq and ne operators for string equal or not; from 8.5, also in and ni operators for list inclusion or not
The argument to expr should in most cases be {braced}. This prevents the Tcl parser from substituting variables in advance, while expr itself has to parse the value from the string. In a braced expression expr can parse variable references itself, and get their numeric value directly where possible. Much faster, usually. The only exception, where bracing should not be used, is if you want to substitute operators from variables:

##### fblocked handle

    returns 1 if the last input operation exhausted all available input, else 0

##### fconfigure handle -option value...

    configure a channel, e.g. its encoding or line-end translation

##### fcopy handle1 handle2

    copy data from handle1 to handle2

##### file

    group of commands dealing with files

##### fileevent

    group of commands dealing with events from channels (readable, writable) but not files

##### flush handle

    make sure the channel's buffer is written out. Useful after puts -nonewline

##### for initbody condition stepbody body

    loop, somehow similar to C's for

##### foreach varlist list ?varlist list...? body

    loop over one or more lists, The varlists can be a single or multiple varNames. Example:

##### format fstring arg..

    put the arguments %-formatted into fstring, similar to C's sprintf

##### gets handle ?varName?

    read a line from handle. If variable is given, assigns the line to it and returns the number of characters read; else returns the line. Guaranteed to be safe against buffer overflows

##### glob ?-options? pattern..

    list of files matching the glob pattern (which can contain * and ? wildcards)

##### global varName..

    declare the given variable(s) as global

##### history

    list the last interactive commands

##### if condition ?then? body1 ?elseif condition body2...? ??else? bodyN?

    conditional

##### incr varName ?amount?

    increments the integer variable by given amount (defaults to 1). Use negative amount to decrement

##### info

    group of commands for introspection

##### interp

    group of commands for interpreters

##### join list ?separator?

    Turn a list into a string, with separator between elements (defaults to " ")

##### lappend varName arg..

    appends the arguments to the list variable. Can also be used to make sure a variable exists:

##### lindex list int..

    retrieve an element from the list by integer index(es)

##### linsert list int arg..

    inserts the arguments at int position into list

##### list ?arg..?

    creates a list from the arguments

##### llength list

    length of the list

##### load filename ?name?

    loads a shared library (DLL)

##### lrange list from to

    returns a sublist at integer indexes from-to

##### lreplace list from to arg..

    replaces the sublist in list with the arguments

##### lsearch ?-options? list element

    searches the list for the element, returns its integer index, or -1 if not found. Can be used to select a subset of elements from a list (using the -all option)

##### lset varName int.. value

    sets an existing element in the named list variable, indexed by integer(s), to the given value

##### lsort ?-options? list

    sorts the list

##### namespace

    group of commands dealing with namespaces

##### open name ?mode ?permissions??

    opens a file or pipe, returns the handle

##### package

    group of commands dealing with packages

##### pid ?handle?

    returns the id of the current process. Can also return the list of pids for a pipeline given the pipeline channel

##### proc name arglist body

    defines a procedure

##### puts ?-nonewline? ?channel? string

    outputs a line to the given channel (default stdout) To prevent errors from closed pipe (like more or head), use

##### pwd

    returns the current working directory

##### read handle ?int?

    reads int bytes from handle (all if int not given)

##### regexp ?-options? re string ?varName...?

    regular expression matching of re in string, possibly assigning parenthesized submatches to the given variables

##### regsub ?-options? re value substring ?varName?

    substitutes occurrences of the regular expression re in value with substring. If varName is given, assigns the new value to it, and returns the number of substitutions; else returns the new value

##### rename cmdName1 cmdName2

    renames a command from cmdName1 to cmdName2, or deletes cmdName1 if cmdName2 is

##### return ?value?

    exits from the current proc or sourced script

##### scan string format ?varName...?

    extracts values by %-format in string to the given variables. Similar to C's sscanf

##### seek channelId offset ?origin?

    moves pointer in file to the given position

##### set varName ?value?

    sets variable to value if given, and returns the variable's value

##### socket ?-myaddr addr? ?-myport myport? ?-async? host port

    open the client side of a TCP connection as a channel

##### socket -server command ?-myaddr addr? port

    open the server side of a TCP connection, register a handler callbacvk command for client requests

##### source filename

    evaluate the contents of the given file

##### split list ?charset?

    splits a string into a list, using any of the characters in charset string as delimiters (defaults to " ")

##### string

    group of commands dealing with strings

##### subst ?-options? string

    performs command, variable, and/or backslash substitutions in string

##### switch ?-options? ?--? value alternatives

    performs one of the alternatives if the value matches

##### tell handle

    return the byte position inside a file

##### time body ?int?

    runs the body for int times (default 1), returns how many microseconds per iteration were used

##### trace

    group of commands to tie actions to variables or commands

##### unset varName..

    delete the given variable(s)

##### update ?idletasks?

    services events

##### uplevel ?level? body

    evaluates the body up in the call stack

##### upvar ?level? varName localVarName...

    ties the given variables up in the call stack to the given local variables. Used for calling by reference, e.g. for arrays

##### variable varName ?value ?varName value...??

    declare the variables as being non-local in a namespace

##### vwait varName

    suspend execution until the given variable has changed. Starts event loop, if not active yet

##### while condition body

    performs body as long as condition is not 0

#### Procedures

Procedures in Tcl cover what other languages call procedures, subroutines, or functions. They always return a result (even if it is the empty string ""), so to call them functions might be most appropriate. But for historical reasons, the Tcl command to create a function is called proc and thus people most often call them procedures.
Examples:
The return is redundant, as the proc returns anyway when reaching its end, and returning its last result:
The following variant is more flexible, as it takes any number of arguments (the special argument name args collects all remaining arguments into a list, which is the value of the parameter args):
An elegant but less efficient alternative builds a string by joining the args with plus signs, and feeds that to expr:
If an argument in a proc definition is a list of two elements, the second is taken as default value if not given in the call ("Sir" in this example):
Introspection: Get the names of all defined procedures with
There are also info subcommands to get the argument list, and possible default arguments, and body of a proc. The following example combines them to recreate the textual form of a proc, given its name (corp being proc in reverse):
Using rename, you can overload any command, including the C-coded ones. First rename the original command to something else, then reimplement it with the same signature, where ultimately the original is called. Here is for instance an overloaded proc that reports if a procedure is defined more than once with same name:
Named arguments: Arguments to commands are mostly by position. But it's very easy to add the behavior known from Python or Ada, that arguments can be named in function calls, which documents the code a bit better, and allows any order of arguments.
The idea (as found in Welch's book) is to use an array (here called "" - the "anonymous array") keyed by argument names. Initially, you can set some default values, and possibly override them with the args of the proc (which has to be paired, i.e. contain an even number of elements):
Usage example:
Testing:

#### Argument passing by name or value

Normally, arguments to commands are passed by value (as constants, or with $ prefixed to a variable name). This securely prevents side-effects, as the command will only get a copy of the value, and not be able to change the variable.
However, sometimes just that is wanted. Imagine you want a custom command to set a variable to zero. In this case, at call time specify the name of the variable (without $), and in the proc use upvar to link the name (in the scope "1 up", i.e. the caller's) to a local variable. I usually put a "_" before arguments that are variable names (e.g. _var), and upvar to the same name without "_" (e.g. var):
If you often use call by reference, you could indicate such arguments with a special pattern (e.g. &arg) and have the following code generate the necessary upvars:
That's all. This command is preferably called first inside a proc, and upvars all arguments that begin with a specific character, the default being "&" - it runs code like
in the caller's scope. Testing:
So the values of a (by value) and b (by reference) are readable; and the side effect of changing b in the caller did also happen:

#### Variable scope

Inside procedures, variables are by default local. They exist only in the proc, and are cleared up on return. However, you can tie local variables to others higher in the call stack (e.g. in the caller), up to the topmost global scope. Examples:

#### Aliases

One can also define a command as an alias to a sequence of one or more words, which will be substituted for it before execution. (The funny arguments are names of the source and target interpreter, which typically is the current one, named by the empty string or ""). Examples:
Introspection: Get the names of all defined aliases with

### Advanced concepts
#### Interpreters

Tcl being an interpreted (plus on-the-fly byte-compiled) language, an interpreter is of course a central kind of object. Every time Tcl is running, at least one interpreter is running, who takes scripts and evaluates them.
One can also create further "slave" interpreters to encapsulate data and processes, which again can have their "sub-slaves", etc., ultimately forming a tree hierarchy. Examples:
By deleting an interpreter, all its global (and namespaced) variables are freed too, so you can use this for modularisation and encapsulation, if needed.
In particular, safe interpreters have intentionally limited capabilities (for instance, access to the file system or the Web) so that possibly malicious code from over the Web cannot create major havoc.
Introspection: The following command lists the sub-interpreters ("slaves") of the current interpreter:

#### Ensembles

Ensembles, (from Tcl 8.5 on), are commands that are composed out of sub-commands according to a standard pattern. Examples include Tcl's built-in chan and clock commands. Dispatching of subcommands, as well as informative error message for non-existing subcommands, is built-in. Subcommands are in a dict structure called "-map", with alternating name and action. Very simple example:
creates a command foo that can be called like
Obviously, ensembles are also a good base for implementing object orientation, where the command is the name of the objects, and the map contains its methods.
Introspection: Serialize an ensemble's map with

#### Namespaces

Namespaces are containers for procedures, non-local variables, and other namespaces. They form a tree structure, with the root at the global namespace named "::". Their names are built with :: as separators too, so ::foo::bar is a child of ::foo, which is a child of :: (similar to pathnames on Unix, where / is the separator as well as the root).
In a nutshell, a namespace is a separate area, or scope, where procedures and variables are visible and private to that scope.
To create a namespace, just evaluate some script (which may be empty) in it:
Now you can use it to define procedures or variables:
To get rid of a namespace (and clean up all its variables, procs, and child namespaces):
Introspection:
The following code gives an approximate size in bytes consumed by the variables and children of a Tcl namespace (of which ::, the global namespace, is of particular interest - all the (grand)*children are also added). If you call this proc repeatedly, you can observe whether data are piling up:
Usage example:

#### Threads

Tcl users have traditionally been skeptical about threads (lightweight concurrent sub-processes) - the event loop model has proved pretty powerful, and simpler to debug. Originally from Tk, the event loop has moved into Tcl and serves
* fileevents (more on channels than real files)
* timed events
* UI events (mouse or keyboard actions by the user)
However, there is a growing tendency to enable threads in Tcl builds. The underlying model is that every thread runs in an interpreter of its own, so it is mostly encapsulated from the rest of the world. Communication between threads must be done with explicit methods.

### Packages and extensions

Packages are Tcl's recommended way to modularize software, especially supportive libraries. The user most often uses the command
One can write packages in pure Tcl, or as wrappers for extensions which come with one or more compiled shared libraries (plus optionally one or more Tcl scripts). Popular extensions are:
* BWidget (adds useful widgets to Tk - more below)
* Expect (supports remote execution over networks)
* Img (adds support of additional image file formats to Tk)
* snack (sound input/output)
* Snit (OO extension, with support for "megawidgets" in Tk)
* sqlite (a tiny yet powerful SQL database)
* tcllib (a collection of pure-Tcl packages - see below)
* TclOO (the canonical object-orientation extension from 8.5)
* tcltcc (a built-in C compiler - see below)
* TclX (collection of system-related extensions, like signal handling)
* tdom (XML parser, SAX or DOM, with XPath query support)
* Tk (the cross-platform GUI toolkit, see below)
* tkcon (a vastly extended console)
* XOTcl (advanced dynamic OO extension)

#### A little example package

The following script creates the trivial but educational package futil, which in a namespace of the same name implements two procs for reading and writing complete text files, and the little introspection helper function, futil::?. The command to register the package (package provide) is executed only after all went well - this way, buggy source code, which raises an error during package require, will not be registered. (Other bugs you'd have to spot and fix for yourself...)
Common Tcl distribution practice has the good habit of profound testing, typically in a separate test directory. On the other hand, including a self-test in the same file with the code makes editing easier, so after the package provide comes a section only executed if this file is sourced as a top- level script, which exercises the commands defined in futil. Whether the string read should be equal to the string written is debatable - the present implementation appends \n to the string if it doesn't end in one, because some tools complain or misbehave if they don't see a final newline.
If the tests do not run into an error either, even the required construction of a package index is fired - assuming the simplified case that the directory contains only one package. Otherwise, you'd better remove these lines, and take care of index creation yourself.
A script that uses this package will only have to contain the two lines
You can even omit the first line, if you install (copy) the directory with the source and pkgIndex.tcl below ${tcl_install_directory}/lib. }
But now back to the single script that makes up the package (it would make sense to save it as futil.tcl). We provide a read and a write function in the futil namespace, plus a little introspection function ? that returns the names of available functions:

#### Tcllib

Tcllib is a collection of packages in pure Tcl. It can be obtained from sourceForge, but is also part of ActiveTcl. The following list of contained packages may not be complete, as Tcllib is steadily growing...
* aes - Advanced Encryption Standard.
* asn - asn.1 BER encoder/decoder
* http/autoproxy - code to automate the use of HTTP proxy servers
* base64 - Base64 encoding and decoding of strings and files.
* bee - BitTorrent serialization encoder/decoder.
* bibtex - Neil Madden's parser for bibtex files. Not fully complete yet, therefore not set for installation.
* calendar - Calendar operations (see also tcllib calendar module).
* cmdline - Various form of command line and option processing.
* comm - Socket based interprocess communication. Emulates the form of Tk's send command.
* control - procedures for tcl flow structures such as assert, do/until, do/while, no-op
* counter - procedures for counters and histograms
* crc - Computation of various CRC checksums for strings and files.
* csv - manipulate comma separated value data
* des - Data Encryption Standard. ::DES::des (not yet installed)
* dns - interact with the Domain Name Service. dns::address, dns::cleanup, dns::cname, dns::configure, dns::name, dns::reset, dns::resolve, dns::status, dns::wait,
* doctools - System for writing manpages/documentation in a simple, yet powerful format.
* exif - exif::analyze exif::fieldnames
* fileutil - Utilities for operating on files, emulating various unix command line applications (cat, find, file(type), touch, ...).
* ftp - Client side implementation of FTP (File Transfer Protocol). In dire need of a rewrite.
* ftpd - Server side implementation of FTP
* grammar_fa - Operations on finite automatons.
* html - generate HTML from a Tcl script. html::author, html::author, html::bodyTag, html::cell, html::checkbox, html::checkSet, html::checkValue, html::closeTag, html::default, html::description, html::description, html::end, html::eval, html::extractParam, html::font, html::for, html::foreach, html::formValue, html::getFormInfo, html::getTitle, html::h, html::h1, html::h2, html::h3, html::h4, html::h5, html::h6, html::hdrRow, html::head, html::head, html::headTag, html::if, html::init, html::init, html::keywords, html::keywords, html::mailto, html::meta, html::meta, html::minorList, html::minorMenu, html::openTag, html::paramRow, html::passwordInput, html::passwordInputRow, html::radioSet, html::radioValue, html::refresh, html::refresh, html::row, html::select, html::selectPlain, html::set, html::submit, html::tableFromArray, html::tableFromList, html::tagParam, html::textarea, html::textInput, html::textInputRow, html::title, html::title, html::urlParent, html::varEmpty, html::while,
* htmldoc - This is not a true module but the place where tcllib 1.3 installed the tcllib documentation in HTML format.
* htmlparse - procedures to permit limited manipulation of strings containing HTML. ::htmlparse::parse, ::htmlparse::debugCallback, ::htmlparse::mapEscapes, ::htmlparse::2tree, ::htmlparse::removeVisualFluff, ::htmlparse::removeFormDefs,
* ident - RFC 1413 ident client protocol implementation
* imap4 - currently undocumented code for interacting with an IMAP server
* inifile - code to manipulate an initialization file. ::ini::open, ::ini::close, ::ini::commit, ::ini::sections, ::ini::keys, ::ini::value
* dns/ip - Manipulation of IP addresses. ::ip::version, ::ip::is, ::ip::normalize, ::ip::equal, ::ip::prefix
* irc - Internet Relay Chat procedures. irc::config, irc::connection,
* javascript - generate Javascript for including in HTML pages. javascript::BeginJS, javascript::EndJS, javascript::MakeMultiSel, javascript::MakeClickProc, javascript::makeSelectorWidget, javascript::makeSubmitButton, javascript::makeProtectedSubmitButton, javascript::makeMasterButton, javascript::makeParentCheckbox, javascript::makeChildCheckbox
* jpeg - edit comment blocks, get image dimensions and information, read exif data of images in the JPG format
* ldap - Client side implementation of LDAP (Lightweight Directory Access Protocol).
* log - general procedures for adding log entries to files ::log::levels, ::log::logMsg, ::log::lv2longform, ::log::lv2color,::log::lv2priority,
* logger - ::logger::walk, ::logger::services, ::logger::enable, ::logger::disable (part of the log module)
* math - general mathematical procedures. ::math::calculus, ::math::combinatorics, ::math::cov, ::math::fibonacci, ::math::integrate, ::math::interpolate, ::math::max, ::math::mean, ::math::min, ::math::optimize, ::math::product, ::math::random, ::math::sigma, ::math::statistics, ::math::stats, ::math::sum
* md4 - ::md4::md4, ::md4::hmac, ::md4::MD4Init, ::md4::MD4Update, ::md4::MD4Final
* md5 - [fill in the description of this module] ::md5::md5, ::md5::hmac, ::md5::test, ::md5::time, ::md5::<<<
* md5crypt - ::md5crypt::md5crypt, ::md5crypt::aprcrypt
* mime - ::mime::initialize, ::mime::parsepart, ::mime::finalize, ::smtp::sendmessage
* multiplexer - [fill in the external interfaces]
* ncgi - procedures for use in a CGI application. ::ncgi::reset, ::ncgi::urlStub, ::ncgi::urlStub
* nntp - routines for interacting with a usenet news server. ::nntp::nntp, ::nntp::NntpProc, ::nntp::NntpProc, ::nntp::okprint, ::nntp::message,
* ntp - network time protocol procedure ::ntp::time
* png - edit comment blocks, get image dimensions and information for Portable Network Graphics format.
* pop3 - Post Office Protocol functions for reading mail from a pop3 server. ::pop3::open, ::pop3::close, ::pop3::status,
* pop3d - Post Office Protocol Server. pop3d::new
* profiler - ::profiler::tZero, ::profiler::tMark, ::profiler::stats, ::profiler::Handler, ::profiler::profProc, ::profiler::init
* rc4 - stream encryption. ::rc4::rc4
* report - format text in various report styles. ::report::report , ::report::defstyle, ::report::rmstyle,
* sha1 - ::sha1::sha1, ::sha1::hmac
* smtpd - ::smtpd::start, ::smtpd::stop, ::smtpd::configure, ::smtpd::cget
* snit - Snit's Not Incr Tcl - OO package. Delegation based. ::snit::type, ::snit::widget, ::snit::widgetadaptor
* soundex::knuth - string matching based on theoretical sound of the letters
* stooop - OO package. stooop::class, stooop::virtual, stooop::new, stooop::delete, stooop::classof
* struct1 - Version 1 of struct (see below), provided for backward compatibility.
       struct::list, ::struct::graph, ::struct::matrix, ::struct::queue, ::struct::stack, ::struct::Tree, ::struct::record, ::struct::skiplist, ::struct::prioqueue, new: ::struct::sets
* tar - untar, list, and stat files in tarballs and create new tarballs
* textutil - Utilities for working with larger bodies of texts. textutil::expand - the core for the expand macro processor.
* tie - Persistence for Tcl arrays.
* treeql - Tree Query Language, inspired by COST.
* uri - Handling of uri/urls (splitting, joining, ...)
* uuid - Creation of unique identifiers.

#### TclOO

TclOO is a loadable package to provide a foundation for object orientation, designed so that specific OO flavors like Itcl, Snit, or XOTcl can build on it. But it's a usable OO system in itself, offering classes, multiple inheritance, mixins and filters. Here is some example code to give you an impression:

#### tcltcc

Tcltcc is a loadable package that wraps the Tiny C compiler (tcc) for use with Tcl. It can be used to
* compile C code directly to memory
* produce dynamic loadable libraries (DLLs) or executable files.
Convenience functions generate wrapper code, so that the user needs only write the really substantial C code.
Examples:
Wrap a C function into a Tcl command "on the fly" (here shown in an interactive session):
Produce a DLL with a fast implementation of Fibonacci numbers:
Produce a tclsh with an extra square command:
Tcltcc is open source, LGPL licensed, available at http://code.google.com/p/tcltcc/ . The full functionality is at the current early stage (October 2007) only available on Windows 95/XP platforms, but in-memory compilation works on Linux too.

#### tDOM

tDOM is a popular extension for XML/HTML processing, allowing both SAX-style "on-the-fly" parsing and the DOM approach of representing a whole XML element in memory.
Here is an example of a SAX-style application. The expat parser that comes with tDOM is instrumented with callbacks for element start, character data, and processing instructions. Elements, attributes, characters and processing instructions are counted, plus a tally for each element type is done.

## expr: the arithmetic & logical unit
### Overview

Arithmetic and logical operations (plus some string comparisons) are in Tcl concentrated in the expr command. It takes one or more arguments, evaluates them as an expression, and returns the result. The language of the expr command (also used in condition arguments of the if, for, while commands) is basically equivalent to C's expressions, with infix operators and functions. Unlike C, references to variables have to be done with $var. Examples:
The difference between Tcl syntax and expr syntax can be contrasted like this:
In another contrast to Tcl syntax, whitespace between "words" is optional (but still recommended for better readability :) And string constants must always be quoted (or braced if you wish):
Then again, Tcl commands can always be embedded into expressions, in square brackets as usual:
In expressions with numbers of mixed types, integers are coerced to doubles:
It is important to know that division of two integers is done as integer division:
You get the (probably expected) floating-point division if at least one operand is double:
If you want to evaluate a string input by the user, but always use floating-point division, just transform it, before the call to expr, by replacing "/" with "*1./" (multiply with floating-point 1. before every division):

### Brace your expressions

In most cases it is safer and more efficient to pass a single braced argument to expr. Exceptions are:
* no variables or embedded commands to substitute
* operators or whole expressions to substitute
The reason is that the Tcl parser parses unbraced expressions, while expr parses that result again. This may result in success for malicious code exploits:
That braced expressions evaluate much faster than unbraced ones, can be easily tested:
The precision of the string representation of floating-point numbers is also controlled by the tcl_precision variable. The following example returns nonzero because the second term was clipped to 12 digits in making the string representation:
while this braced expression works more like expected:

### Operators

Arithmetic, bitwise and logical operators are like in C, as is the conditional operator found in other languages (notably C):
* c?a:b -- if c is true, evaluate a, else b
The conditional operator can be used for compact functional code (note that the following example requires Tcl 8.5 so that fac can be called inside its own definition):

#### Arithmetic operators

The arithmetic operators are also like those found in C:
* + addition
* - (binary: subtraction. unary: change sign)
* * multiplication
* / (integer division if both operands are integer
* % (modulo, only on integers)
* ** power (available from Tcl 8.5)

#### Bitwise operators

The following operators work on integers only:
* & (AND)
* | (OR)
* ^ (XOR)
* ~ (NOT)
* << shift left
* >> shift right

#### Logical operators

The following operators take integers (where 0 is considered false, everything else true) and return a truth value, 0 or 1:
* && (and)
* || (or)
* ! (not - unary)

#### Comparison operators

If operands on both side are numeric, these operators compare them as numbers. Otherwise, string comparison is done. They return a truth value, 0 (false) or 1 (true):
* == equal
* != not equal
* > greater than
* >= greater or equal than
* < less than
* <= less or equal than
As truth values are integers, you can use them as such for further computing, as the sign function demonstrates:

#### String operators

The following operators work on the string representation of their operands:
* eq string-equal
* ne not string-equal
Examples how "equal" and "string equal" differ:

#### List operators

From Tcl 8.5, the following operators are also available:
* a in b - 1 if a is a member of list b, else 0
* a ni b - 1 if a is not a member of list b, else 0
Before 8.5, it's easy to write an equivalent function
Usage example:
which you can rewrite, once 8.5 is available wherever your work is to run, with

### Functions

The following functions are built-in:
* abs(x) - absolute value
* acos(x) - arc cosine. acos(-1) = 3.14159265359 (Pi)
* asin(x) - arc sine
* atan(x) - arc tangent
* atan2(y,x)
* ceil(x) - next-highest integral value
* cos(x) - cosine
* cosh(x) - hyperbolic cosine
* double(x) - convert to floating-point number
* exp(x) - e to the x-th power. exp(1) = 2.71828182846 (Euler number, e)
* floor(x) - next-lower integral value
* fmod(x,y) - floating-point modulo
* hypot(y,x) - hypotenuse (sqrt($y*$y+$x*$x), but at higher precision)
* int(x) - convert to integer (32-bit)
* log(x) - logarithm to base e
* log10(x) - logarithm to base 10
* pow(x,y) - x to the y-th power
* rand - random number > 0.0 and < 1.0
* round(x) - round a number to nearest integral value
* sin(x) - sine
* sinh(x) - hyperbolic sine
* sqrt(x) - square root
* srand(x) - initialize random number generation with seed x
* tan(x) - tangent
* tanh(x) - hyperbolic tangent
* wide(x) - convert to wide (64-bit) integer
Find out which functions are available with info functions:

### Exporting expr functionalities

If you don't want to write [[expr {$x+5}]] every time you need a little calculation, you can easily export operators as Tcl commands:
After that, you can call these operators like in LISP:
Of course, one can refine this by allowing variable arguments at least for + and *, or the single-argument case for -:
Similarly, expr functions can be exposed:
In Tcl 8.5, the operators can be called as commands in the ::tcl::mathop namespace:
You can import them into the current namespace, for shorthand math commands:

### User-defined functions

From Tcl 8.5, you can provide procs in the ::tcl::mathfunc namespace, which can then be used inside expr expressions:
This is especially useful for recursive functions, or functions whose arguments need some expr calculations:

## Interaction and debugging

Tcl itself is quite a good teacher. Don't be afraid to do something wrong - it will most often deliver a helpful error message. When tclsh is called with no arguments, it starts in an interactive mode and displays a "%" prompt. The user types something in and sees what comes out: either the result or an error message.
Trying isolated test cases interactively, and pasting the command into the editor when satisfied, can greatly reduce debugging time (there is no need to restart the application after every little change - just make sure it's the right one, before restarting.)

### A quick tour

Here's a commented session transcript:
OK, so we're supposed to type in a command. Although it doesn't look so, here's one:
Interactive tclsh tries to guess what we mean, and "hi" is the unambiguous prefix of the "history" command, whose results we see here. Another command worth remembering is "info":
The error message tells us there should be at least one option, and optionally more arguments.
Another helpful error: "option" is not an option, but the valid ones are listed. To get information about commands, it makes sense to type the following:
Oh my, quite many... How many?
Now for a more practical task - let's let Tcl compute the value of Pi.
Hm.. can we have that with more precision?
Back to the first try, where "hello" was an invalid command. Let's just create a valid one:
Silently acknowledged. Now testing:

### Errors are exceptions

What in Tcl is called error is in fact more like an exception in other languages - you can deliberately raise an error, and also catch errors. Examples:
One reason for errors can be an undefined command name. One can use this playfully, together with catch, as in the following example of a multi-loop break, that terminates the two nested loops when a matrix element is empty:
The throw command does not exist in normal Tcl, so it throws an error, which is caught by the catch around the outer loop.

#### The errorInfo variable

This global variable provided by Tcl contains the last error message and the traceback of the last error. Silly example:
If no error has occurred yet, errorInfo will contain the empty string.

#### The errorCode variable

In addition, there is the errorCode variable that returns a list of up to three elements:
* category (POSIX, ARITH, ...)
* abbreviated code for the last error
* human-readable error text
Examples:

### Tracing procedure calls

For a quick overview how some procedures are called, and when, and what do they return, and when, the trace execution is a valuable tool. Let's take the following factorial function as example:
We need to supply a handler that will be called with different numbers of arguments (two on enter, four on leave). Here's a very simple one:
Now we instruct the interpreter to trace enter and leave of fac:
Let's test it with the factorial of 7:
which gives, on stdout:
So we can see how recursion goes down to 1, then returns in backward order, stepwise building up the final result. The 0 that comes as second word in "leave" lines is the return status, 0 being TCL_OK.

### Stepping through a procedure

To find out how exactly a proc works (and what goes wrong where), you can also register commands to be called before and after a command inside a procedure is called (going down transitively to all called procs). You can use the following step and interact procedures for this:
The above code gives the following transcript when sourced into a tclsh:

### Debugging

The simplest way to inspect why something goes wrong is inserting a puts command before the place where it happens. Say if you want to see the values of variables x and y, just insert
(if the string argument contains no spaces, it needs not be quoted). The output will go to stdout - the console from where you started the script. On Windows or Mac, you might need to add the command
to get the substitute console Tcl creates for you, when no real one is present.
If at some time you want to see details of what your program does, and at others not, you can define and redefine a dputs command that either calls puts or does nothing:
For more debugging comfort, add the proc interact from above to your code, and put a call to interact before the place where the error happens. Some useful things to do at such a debugging prompt:

### Assertions

Checking data for certain conditions is a frequent operation in coding. Absolutely intolerable conditions can just throw an error:
Where the error occurred is evident from ::errorInfo, which will look a bit clearer (no mention of the error command) if you code
If you don't need hand-crafted error messages, you can factor such checks out to an assert command:
Use cases look like this:
Note that the condition is reverted - as "assert" means roughly "take for granted", the positive case is specified, and the error is raised if it is not satisfied.
Tests for internal conditions (that do not depend on external data) can be used during development, and when the coder is sure they are bullet-proof to always succeed, (s)he can turn them off centrally in one place by defining
This way, assertions are compiled to no bytecode at all, and can remain in the source code as a kind of documentation.
If assertions are tested, it only happens at the position where they stand in the code. Using a trace, it is also possible to specify a condition once, and have it tested whenever a variable's value changes:
The ";#" at the end of the trace causes the additional arguments name element op, that are appended to the command prefix when a trace fires, to be ignored as a comment.
Testing:
The error message isn't as clear as could be, because the [llength $list] is already substituted in it. But I couldn't find an easy solution to that quirk in this breakfast fun project - backslashing the $condition in the assertt code sure didn't help. Better ideas welcome.
To make the assertion condition more readable, we could quote the condition one more time,i.e
In this case,when trace trigger fires, the argument for assert is {[llength $list]<10}.
In any case, these few lines of code give us a kind of bounds checking - the size of Tcl's data structures is in principle only bounded by the available virtual memory, but runaway loops may be harder to debug, compared to a few assertt calls for suspicious variables:
or
Tcllib has a control::assert with more bells and whistles.

### A tiny testing framework

Bugs happen. The earlier found, the easier for the coder, so the golden rule "Test early. Test often" should really be applied.
One easy way is adding self-tests to a file of Tcl code. When the file is loaded as part of a library, just the proc definitions are executed. If however you feed this file directly to a tclsh, that fact is detected, and the "e.g." calls are executed. If the result is not the one expected, this is reported on stdout; and in the end, you even get a little statistics.
Here's a file that implements and demonstrates "e.g.":

### Guarded proc

In more complex Tcl software, it may happen that a procedure is defined twice with different body and/or args, causing hard-to-track errors. The Tcl command proc itself doesn't complain if it is called with an existing name. Here is one way to add this functionality. Early in your code, you overload the proc command like this:
From the time that is sourced, any attempt to override a proc name will be reported to stderr (on Win-wish, it would show on the console in red). You may make it really strict by adding an "exit" after the "puts stderr ...", or throw an error.
Known feature: proc names with wildcards will run into this trap, e.g.
will always lead to a complaint because "*" fits any proc name. Fix (some regsub magic on 'name') left as an exercise.

### Windows wish console

While on Unixes, the standard channels stdin, stdout, and stderr are the same as the terminal you started wish from, a Windows wish doesn't typically have these standard channels (and is mostly started with double-click anyway). To help this, a console was added that takes over the standard channels (stderr even coming in red, stdin in blue). The console is normally hidden, but can be brought up with the command
You can also use the partially documented "console" command. "console eval <script>" evals the given script in the Tcl interpreter that manages the console. The console's text area is actually a text widget created in this interpreter. For example:
will change the font of the console to "Times". Since the console is a Tk text widget, you can use all text widget commands and options on it (for example, changing colors, bindings...).
tells you more about the console widget: it is a toplevel with children .menu, .console (text), and .sb (scrollbar). You can resize the whole thing with
where $W and $H are dimensions in character cells (default 80x24), but $X and $Y are in pixels.
And more again: you can even add widgets to the console - try
The button appears between the text widget and the scroll bar, and looks and does as expected. There is also a way back: the main interpreter is visible in the console interpreter under the name, consoleinterp.

### Remote debugging

Here's a simple experiment on how to connect two Tcl processes so that one (call it "debugger") can inspect and control the other ("debuggee"). Both must have an event loop running (which is true when Tk runs, or when started with e.g. vwait forever).
As this goes over a socket connection, the two processes could be on different hosts and operating systems (though I've so far tested only the localhost variety). Use at your own risk, of course... :^)
The "debuggee" contains in my experiments the following code, in addition to its own:
The "debugger" in this version (remo.tcl) runs only on Windows in a wish, as it needs a console, but you could modify it to avoid these restrictions:
Now from remo you can call any Tcl command in the "debuggee", where it is executed in global scope, so in particular you can inspect (and modify) global variables. But you could also redefine procs on the fly, or whatever tickles your fancy... Examples from a remo session, showing that the two have different pids, how errors are reported, and that quoting is different from normal (needs more work):

## Tcl in internationalization

"Everything is a string", the Tcl mantra goes. A string is a (finite-length) sequence of characters. Now, what is a character? A character is not the same as a glyph, the writing element that we see on screen or paper - that represents it, but the same glyph can stand for different characters, or the same character be represented with different glyphs (think e.g. of a font selector).
Also, a character is not the same as a byte, or sequence of bytes, in memory. That again may represent a character, but not unequivocally, once we leave the safe haven of ASCII.
Let's try the following working definition: "A character is the abstract concept of a small writing unit". This often amounts to a letter, digit, or punctuation sign - but a character can be more or less than that. More: Ligatures, groups of two or more letters, can at times be treated as one character (arranged even in more than one line, as seen in Japanese U+337F ㍿ or Arabic U+FDFA ﷺ). Less: little marks (diacritics) added to a character, like the two dots on ü in Nürnberg (U+00FC), can turn that into a new "precomposed" character, as in German; or they may be treated as a separate, "composing character" (U+0308 in the example) which in rendering is added to the preceding glyph (u, U+0075) without advancing the rendering position - a more sensible treatment of the function of these two dots, "trema", in Spanish, Dutch, or even (older) English orthography: consider the spelling "coöperation" in use before c. 1950. Such composition is the software equivalent of "dead keys" on a typewriter.
Although an abstract concept, a character may of course have attributes, most importantly a name: a string, of course, which describes its function, usage, pronunciation etc. Various sets of names have been formalized in Postscript (/oumlaut) or HTML (&ouml;). Very important in technical applications is of course the assignment of a number (typically a non-negative integer) to identify a character - this is the essence of encodings, where the numbers are more formally called code points. Other attributes may be predicates like "is upper", "is lower", "is digit".
The relations between the three domains are not too complicated: an encoding controls how a 1..n sequence of bytes is interpreted as a character, or vice versa; the act of rendering turns an abstract character into a glyph (typically by a pixel or vector pattern). Conversely, making sense of a set of pixels to correctly represent a sequence of characters, is the much more difficult art of OCR, which will not be covered here.

### Pre-Unicode encodings

Work on encodings, mapping characters to numbers (code points), has a longer history than electronic computing. Francis Bacon (1561-1626) is reported to have used, around 1580, a five-bit encoding where bits were represented as "a" or "b", of the English/Latin alphabet (without the letters j and u!), long before Leibniz discussed binary arithmetics in 1679. An early encoding in practical use was the 5-bit Baudot/CCIT-2 teletype (punch tape) code standardized in 1932, which could represent digits and some punctuations by switching between two modes. I have worked on Univac machines that used six bits per "Fieldata" character, as hardware words were 36 bits long. While IBM used 8 bits in the EBCDIC code, the more famous American Standard Code for Information Interchange (ASCII) did basically the same job in 7 bits per character, which was sufficient for upper/lowercase basic Latin (English) as well as digits and a number of punctuations and other "special" characters - as hardware tended to 8-bit bytes as smallest memory unit, one was left for parity checks or other purposes.
The most important purpose, outside the US, was of course to accommodate more letters required to represent the national writing system - Greek, Russian, or the mixed set of accented or "umlauted" characters used in virtually every country in Europe. Even England needed a code point for the Pound Sterling sign. The general solution was to use the 128 additional positions available when ASCII was implemented as 8-bit bytes, hex 80..FF. A whole flock of such encodings were defined and used:
* ISO standard encodings iso8859-.. (1-15)
* MS/DOS code pages cp...
* Macintosh code pages mac...
* Windows code pages cp1...

### East Asian encodings

The East Asian countries China, Japan, and Korea all use character sets numbering in the thousands, so the "high ASCII" approach was not feasible there. Instead, the ASCII concept was extended to a 2x7 bit pattern, where the 94 printing ASCII characters indicate row and column in a 94x94 matrix. This way, all character codes were in practice two bytes wide, and thousands of Hanzi/Kanji/Hangul could be accommodated, plus hundreds of others (ASCII, Greek, Russian alphabets, many graphic characters). These national multibyte encodings are:
* JIS C-6226 (Japan, 1978; significantly revised 1983, 1990)
* GB 2312-80 (Mainland China, 1980)
* KS C-5601 (South Korea, 1987)
If the 2x7 pattern was directly implemented, files in such encodings could not be told apart from ASCII files, except for unreadability. However, it does get some use in Japanese e-mails (in prevailing conventions developed prior to 8-bit-clean mail servers), with ANSI escape codes using the ESC control character being used to declare sections of text as ASCII or JIS C-6226, in what has become known as "JIS encoding" (or more properly as ISO-2022-JP).
Elsewhere, in order to handle both types of strings transparently in a more practical manner, the "high ASCII" approach was extended so that a byte in 00..7F was taken at ASCII face value, while bytes with high bit set (80..FF) were interpreted as halves of multibyte codes. For instance, the first Chinese character in GB2312, row 16 col 1 (decimally 1601 for short), gives the two bytes
This implementation became known as "Extended UNIX Code" (EUC) in the national flavors euc-cn (China), -jp (Japan), -kr (Korea).
To add to the "ideograph soup" confusion, unlike euc-cn and euc-kr, euc-jp (Japan) was not widely adopted on the Windows or Macintosh platforms, which instead tend to use the incompatible ShiftJIS, which re-arranges the codes to make space for older single-byte codes for phonetic katakana characters. Also, Taiwan and Hong Kong use their own "Big 5" encoding, which doesn't use the 94×94 structure. Unlike EUC encodings, ASCII bytes can appear as the second byte of two-byte codes in these encodings, which is also true of common extensions to the EUC encodings (GBK extending euc-cn, Unified Hangul Code extending euc-kr).

### Unicode as pivot for all other encodings

The Unicode standard is an attempt to unify all modern character encodings into one consistent 16-bit representation. Consider a page with a 16x16 table filled with EuroLatin-1 (ISO 8859-1), the lower half being the ASCII code points. Call that "page 00" and imagine a book of 256 or more such pages (with all kinds of other characters on them, in majority CJK), then you have a pretty clear concept of the Unicode standard, in which a character's code position is "U+" hex (page number*256+cell number), for instance, U+20A4 is a version of the Pound Sterling sign. Initiated by the computer industry (www.unicode.org), the Unicode has grown together with ISO 10646, a parallel standard providing an up-to-31-bits encoding (one left for parity?) with the same scope. Software must allow Unicode strings to be fit for i18n. From Unicode version 3.1, the 16-bit limit was transcended for some rare writing systems, but also for the CJK Unified Ideographs Extension B - apparently, even 65536 code positions are not enough. The total count in Unicode 3.1 is 94,140 encoded characters, of which 70,207 are unified Han ideographs; the next biggest group are over 14000 Korean Hangul. And the number is growing.

### Unicode implementations: UTF-8, UCS-2/UTF-16

UTF-8 is made to cover 7-bit ASCII, Unicode, and ISO 10646. Characters are represented as sequences of 1..6 eight-bit bytes - termed octets in the character set business - (for ASCII: 1, for Unicode: 2..4) as follows:
* ASCII 0x00..0x7F (Unicode page 0, left half): 0x00..0x7F. Nothing changed.
* Unicode, pages 00..07: 2 bytes, 110aaabb 10bbbbbb, where aaa are the rightmost bits of page#, bb.. are the bits of the second Unicode byte. These pages cover European/Extended Latin, Greek, Cyrillic, Armenian, Hebrew, Arabic.
* Unicode, pages 08..FE: 3 bytes, 1110aaaa 10aaaabb 10bbbbbb. These cover the rest of the Basic Multilingual Plane, including Hangul, Kanji, and what else. This means that East Asian texts are 50% longer in UTF-8 than in pure 16 bit Unicode.
* Unicode, supplementary planes: 4 bytes, 11110ppp 10ppaaaa 10aaaabb 10bbbbbb. These were not part of the original design of Unicode (only ISO 10646), but they were added to the Unicode standard when it became clear that one plane would not be sufficient for Unicode's goals. They mostly cover Emoji, ancient writing systems, niche writing systems, enormous numbers of obscure Kanji/Hanzi, and a few very new writing systems which postdate the original design of Unicode.
* ISO 10646 codes beyond Unicode: 4..6 bytes. Since the current approval process keeps the standards in sync by preventing ISO 10646 being allocated beyond the 17 Unicode planes, these are guaranteed not to exist in the foreseeable future.
The general principle of UTF-8 is that the first byte either is a single-byte character (if below 0x80), or indicates the length of a multi-byte code by the number of 1's before the first 0, and is then filled up with data bits. All other bytes start with bits 10 and are then filled up with 6 data bits. It follows from this that bytes in UTF-8 encoding fall in distinct ranges:
The distinction between initial and non-initial helps in plausibility checks, or to re-synchronize with missing data. Besides, it's independent of byte order (as opposed to UCS-16, see below). Tcl however shields these UTF-8 details from us: characters are just characters, no matter whether 7 bit, 16 bit, or (in the future) more.
The byte sequence EF BB BF is the UTF-8 equivalent of \uFEFF, which is detected by Windows Notepad, which switches to the UTF-8 encoding when a file starts with these three bytes, and writes them when saving a file as UTF-8. This isn't always used elsewhere, but will generally override an otherwise declared character encoding if a file starts with it.
The UCS-2 representation (in Tcl just called the "unicode" encoding) is much more easily explained: each character code is written as a 16-bit "short" unsigned integer. The practical complication is that the two memory bytes making up a "short" can be arranged in "big-endian" (Motorola, Sparc) or "little-endian" (Intel) byte order. Hence, the following rules were defined for Unicode:
* Code point U+FEFF was defined as Byte Order Mark (BOM), later renamed to "Zero-width non-breaking space", although actually using it in its secondary whitespace role is now considered obsolete.
* Code point U+FFFE (as well as FFFF) is a guaranteed non-character, and will never be a valid Unicode character. They are intended for use as sentinels, or for other internal use, as well as for detecting if the Byte Order Mark is being read incorrectly.
This way, a Unicode-reading application (even Notepad/W2k) can easily detect that something's wrong when it encounters the byte sequence FFFE, and swap the following byte pairs - a minimal and elegant way of dealing with varying byte orders.
While Unicode was originally intended to fit entirely within UCS-2, with the entirety of ISO 10646 requiring a 32-bit "long" (so-called UCS-4 or UTF-32), this distinction was later scrapped since one sixteen-bit plane was no longer considered sufficient to achieve Unicode's goal. Therefore, sixteen "supplementary" planes were added to Unicode, with the original 16-bit plane being kept as the "Plane 0" or "Basic Multilingual Plane". In order to use characters from supplementary planes in interfaces expecting a UCS-2 stream, the range U+D800–U+DFFF was guaranteed never to be used for Unicode characters. This allows characters in supplementary planes to be unambiguously represented in an otherwise UCS-2 stream, this is known as UTF-16.
A supplementary character is represented in big-endian UTF-16 as follows, where ssss represents one less than the plane number. In little-endian, the first two bytes are swapped and the last two bytes are swapped, the entire sequence isn't reversed. This because it is treated like a sequence of two UCS-2 characters.
For XML, an encoding self-identification is defined with the encoding attribute in the leading tag. This is only useful for documents which can be treated as ASCII up to that point though, so UTF-16/UCS-2 has to be detected beforehand or otherwise indicated.

### Tcl and encodings

From Tcl 8.1, i18n support was brought to string processing, and it was wisely decided to
* use Unicode as general character set
* use UTF-8 as standard internal encoding
* provide conversion support for the many other encodings in use.
However, as unequal-length byte sequences make simple tasks as indexing into a string, or determining its length in characters more complex, the internal representation is converted to fixed-length 16-bit UCS-16 in such cases. (This brings new problems with recent Unicodes that cross the 16-bit barrier... When practical use justifies it, this will have to change to UCS-32, or 4 bytes per character.)
Not all i18n issues are therefore automatically solved for the user. One still has to analyze seemingly simple tasks like uppercase conversion (Turkish dotted/undotted I make an anomaly) or sorting ("collation order" is not necessarily the numeric order of the Unicodes, as lsort would apply by default), and write custom routines if a more correct behavior is required. Other locale-dependent i18n issues like number/currency formatting, date/time handling also belong to this group. I recommend to start from the defaults Tcl provides, and if necessary, customize the appearance as desired. International data exchange is severely hampered if localized numeric data are exchanged, one side using period, the other comma as decimal point...
Strictly spoken, the Tcl implementation "violates the UTF-8 spec, which explicitly forbids non-canonical representation of characters and requires that malformed UTF-8 sequences in the input be errors. ... I think that to be an advantage. But the spec says 'MUST' so we're at least technically non-compliant." (Kevin B. Kenny in the Tcl chat, 2003-05-13)
If textual data are internal to your Tcl script, all you have to know is the \uxxxx notation, which is substituted into the character with Unicode U+xxxx (hexadecimal). This notation can be used wherever Tcl substitution takes place, even in braced regexp's and string map pairlists; else you can force it by substing the string in question.
To demonstrate that for instance scan works transparently, here's a one-liner to format any Unicode character as HTML hex entity:
Conversely it takes a few lines more:
For all other purposes, two commands basically provide all i18n support:
enables conversion from/to encoding e for an open channel (file or socket) if different from system encoding;
does what it says, the other encoding being always Unicode.
For instance, I could easily decode the bytes EF BB BF from a hexdump with
in an interactive tclsh, and found that it stood for the famous byte-order mark FEFF. Internally to Tcl, (almost) everything is a Unicode string. All communications with the operating system is done in the "system encoding", which you can query (but best not change) with the [encoding system] command. Typical values are iso8859-1 or -15 on European Linuxes, and cp1252 on European Windowses.
Introspection: Find out what encodings are available in your installation with
You can add new encodings by producing an .enc file and copying that in the directory lib/tcl8.4/encoding (or similar) where the other .enc files are situated. For the format of encoding files (which are text files, consisting mostly of hex digits), see the man page http://www.tcl.tk/man/tcl8.4/TclLib/Encoding.htm . The basename of your .enc file (without the .enc extension) will be the name under which it can be addressed, e.g. for an encoding iso4711 name the file iso4711.enc.

### Localization: message catalogs

Finally, the msgcat package supports localization ("l10n") of apps by allowing message catalogs for translation of strings, typically for GUI display, from a base language (typically English) to a target language selected by the current locale. For example, an app to be localized for France might contain a file en_fr.msg with, for simplicity, only the line
In the app itself, all you need is
to have the button display the localized text for "File", namely "Fichier", as obtained from the message catalog. For other locales, only a new message catalog has to be produced by translating from the base language. Instead of explicit setting as in (1), typically the locale information might come from an environment (LANG) or registry variable.

### Tk: text rendering, fonts

Rendering international strings on displays or printers can pose the biggest problems. First, you need fonts that contain the characters in question. Fortunately, more and more fonts with international characters are available, a pioneer being Bitstream Cyberbit that contains roughly 40000 glyphs and was for some time offered for free download on the Web. Microsoft's Tahoma font also added support for most alphabet writings, including Arabic. Arial Unicode MS delivered with Windows 2000 contains just about all the characters in the Unicode, so even humble Notepad can get truly international with that.
But having a good font is still not enough. While strings in memory are arranged in logical order, with addresses increasing from beginning to end of text, they may need to be rendered in other ways, with diacritics shifted to various positions of the preceding character, or most evident for the languages that are written from right to left ("r2l"): Arabic, Hebrew. (Tk still lacks automatic "bidi"rectional treatment, so r2l strings have to be directed "wrongly" in memory to appear right when rendered - see A simple Arabic renderer on the Wiki).
Correct bidi treatment has consequences for cursor movement, line justification, and line wrapping as well. Vertical lines progressing from right to left are popular in Japan and Taiwan - and mandatory if you had to render Mongolian.
Indian scripts like Devanagari are alphabets with about 40 characters, but the sequence of consonants and vowels is partially reversed in rendering, and consonant clusters must be rendered as ligatures of the two or more characters involved - the pure single letters would look very ugly to an Indian. An Indian font for one writing system already contains several hundred glyphs. Unfortunately, Indian ligatures are not contained in the Unicode (while Arabic ones are), so various vendor standards apply for coding such ligatures.

#### A little i18n tester

Here's a little script that shows you what exotic characters your system has available. It creates a text window and tries to show some sample text for the specified languages (the screenshot is from a PocketPC in the Bitstream Cyberbit font):
No font or size are specified, so you see the pure defaults (and notice how Tk manages to find characters). You can then configure the text widget for the fonts you'd like to see.

#### Input methods in Tcl/Tk

To get outlandish characters not seen on the keyboard into the machine, they may at lowest level be specified as escape sequences, e.g. "\u2345". But most user input will come from keyboards, for which many layouts exist in different countries. In CJK countries, there is a separate coding level between keys and characters: keystrokes, which may stand for the pronunciation or geometric components of a character, are collected in a buffer and converted into the target code when enough context is available (often supported by on-screen menus to resolve ambiguities).
Finally, a "virtual keyboard" on screen, where characters are selected by mouse click, is especially helpful for non-frequent use of rarer characters, since the physical keyboard gives no hints which key is mapped to which other code. This can be implemented by a set of buttons, or minimally with a canvas that holds the provided characters as text items, and bindings to <1>, so clicking on a character inserts its code into the widget which has keyboard focus. See iKey: a tiny multilingual keyboard.
The term "input methods" is often used for operating-system-specific i18n support, but I have no experiences with this, doing i18n from a German Windows installation. So far I'm totally content with hand-crafted pure Tcl/Tk solutions - see taiku on the Wiki.

### Transliterations: The Lish family

The Lish family is a set of transliterations, all designed to convert strings in lowly 7-bit ASCII to appropriate Unicode strings in some major non-Latin writing systems. The name comes from the common suffix "lish" as in English, which is actually the neutral element of the family, faithfully returning its input ;-) Some rules of thumb:
* One *lish character should unambiguously map to one target character, wherever applicable
* One target letter should be represented by one *lish letter (A-Za-z), wherever applicable. Special characters and digits should be avoided for coding letters
* Mappings should be intuitive and/or follow established practices
* In languages that distinguish case, the corresponding substitutes for upper- and lowercase letters should also correspond casewise in lower ASCII.
The Tclers' Wiki http://mini.net/tcl/ has the members of the Lish family available for copy'n'paste. The ones I most frequently use are
* Arblish, which does context glyph selection and right-to-left conversion;
* Greeklish;
* Hanglish for Korean Hangul, which computes Unicodes from initial-vowel-final letters;
* Ruslish for Cyrillic.
Calling examples, that return the Unicodes for the specified input:

#### Greeklish

It all began with Greeklish, which is not my invention, but used by Greeks on the Internet for writing Greek without Greek fonts or character set support. I just extended the practice I found with the convention of marking accented vowels with a trailing apostrophe (so it's not a strict 1:1 transliteration anymore). Special care was taken to convert "s" at word end to "c", so it produces the final-sigma. Here is the code:
Testing:

#### Hanglish

Even though the Korean Hangul writing has many thousands of syllable characters, it is possible to compute the Unicode from the spelling of a syllable and vice versa. Here's how:

### Collation

Collation is "the logical ordering of character or wide-character strings according to defined precedence rules. These rules identify a collation sequence between the collating elements, and such additional rules that can be used to order strings consisting of multiple collating elements."
Tcl's lsort sorts according to numerical Unicode values, which may not be correct in some locales. For instance, in Portuguese, accented letters should sort as if they weren't, but in Unicode sequence come after "z".
The following simple code takes a map in which collation differences can be listed as {from to from to...}, sorts the mapped items, and retrieves only the original elements:
Testing, Portuguese:
Spanish (ll sorts after lz):
German (umlauts sorted as if "ä" was "ae"):

## Regular expressions
### Overview

Another language (I wouldn't want to call it "little") embedded inside Tcl is regular expressions. They may not look like this to you - but they follow (many) rules, indeed. The purpose is to describe a string pattern to match with - for searching, extracting, or replacing substrings.
Regular expressions are used in the regexp, regsub commands, and optionally in lsearch and switch. Note that this language is very different from Tcl itself, so in most cases it is best to brace an RE, to prevent the Tcl parser from misunderstanding them.
Before the gory details begin, let's start with some examples:
returns 1 if $input contains one or more digits, followed by a lowercase letter.
deletes all uppercase letters from $input, and saves that to the result variable.
returns all elements in the list $input which start with a "-".

### Character classes

Many characters just stand for themselves. E.g.
matches indeed the character "a". Any Unicode can be specified in the \uXXXX format. In brackets (not the same as Tcl's), a set of alternatives (a "class") is defined:
matches "a", "b", or "c". A dash (-) between two characters spans the range between them, e.g.
matches one decimal digit. To have literal "-" in a set of alternatives, put it first or last:
matches a digit or a minus sign. A bracketed class can be negated by starting it with ^, e.g.
matches any character that is not a decimal digit. The period "." represents one instance of any character. If a literal "." is intended (or in general, to escape any character that has a special meaning to the regexp syntax), put a backslash "\" before it - and make sure the regular expression is braced, so the Tcl parser doesn't eat up the backslash early...).

### Quantifiers

To repeat a character (set) other than once, there are quantifiers put behind it, e.g.
There is also a way of numeric quantification, using braces (again, not the same as Tcl's):
The + and * quantifiers act "greedy", i.e. they consume the longest possible substring. For non-greedy behavior, which provides the shortest possible match, add a "?" in behind. Examples:
This matches until the last close-bracket
This matches until the first close-bracket.

### Anchoring

By default, a regular expression can match anywhere in a string. You can limit that to the beginning (^) and/or end ($):
succeeds if input begins with "a" and ends with "z", and has one or more characters between them.

### Grouping

A part of a regular expression can be grouped by putting parentheses around it. This can have several purposes:
* regexp and regsub can extract or refer to such substrings
* the operator precedence can be controlled
The "|" (or) operator has high precedence. So
matches the strings "foo" or "bar grill", while
matches "foo grill" or "bar grill".
For extracting substrings into separate variables, regexp allows additional arguments:
Here, variable fullmatch will receive the substring of input that matched the regular expression re, while part1 etc. receive the parenthesized submatches. As fullmatch is often not needed, it has become an eye-candy idiom to use the variable name "->" in that position, e.g.
places the first two characters of input in the variable first, and the next three in the variable second. If $input was "ab123", first will hold "ab", and second will contain "123".
In regsub and regexp, you can refer to parenthesized submatches with \1 for the first, \2 for the second, etc. \0 is the full match, as with regexp above. Example:
Here \1 contains "ab", \2 contains "123", and \0 is the full match "ab123". Another example, how to find four times the same lowercase letter in a row (the first occurrence, plus then three):

### More examples

Parse the contents in angle brackets (note that the result contains the full match and the submatch in paired sequence, so use foreach to extract only the submatches):
Insert commas between groups of three digits into the integer part of a number:
In other countries, you might use an apostrophe (') as separator, or make groups of four digits (used in Japan).
In the opposite direction, converting such formatted numbers back to the regular way for use in calculations, the task consists simply of removing all commas. This can be done with regsub:
but as the task involves only constant strings (comma and empty string), it is more efficient not to use regular expressions here, but use a string map command:

## Working with files
### Files and channels

In addition to the functionalities known from C's stdio, Tcl offers more commands that deal with files, similar to what shells provide, though often a bit more verbose. Here are some examples:
List all files in the current directory that match the pattern *.tcl.
To let code temporarily execute in another directory, use this pattern:
More precisely, many "file" operations work in general on "channels", which can be
* standard channels (stdin, stdout, stderr)
* files as opened with open ...
* pipes as opened with 'open |...
* sockets (TCP)

### File names

Files are often addressed with path names, which indicate their position in the directory tree. Like Unix, Tcl uses "/" as path separator, while on Windows "\" is the native way - which brings trouble not only to Tcl, but even to C, because "\" is the escape character on both, so that e.g. \t is parsed as horizontal tab, \n as newline, etc. Fortunately Windows accepts natively "/" as well, so you can use forward slash in both Tcl and C programs as path separator without worries. However, you still have to take care of escape sequences. One stopgap measure is
* to escape the escape character, i.e. write \\ for \, or
* brace backslashed names, e.g. {\foo\bar\grill.txt}
But Tcl allows to use the "normal" separator / in almost all situations, so you're safer off with that. Unfortunately, things are sad for Mac users, since MacOS (before X) accepts only ":" as file separator.
If you need to, here's ways to convert between the two forms:
You can even use file join command: file join arg1 arg2 ... argN
Tcl will then take care of all platform dependent details to create platform independent path. For example:
set somepath [file join foo bar grill.txt]
will result in following path (on windows machine): foo/bar/grill.txt

### Input and output

Tcl's input/output commands are pretty closely based on those from C's stdio (just strip off the leading f):
* set handle [open filename ?mode?]
* set data [read $handle ?int?]
* tell $handle
* seek $handle offset ?from?
* puts ?-nonewline? ?$handle? content
* gets $handle ?varname?
* close $handle
C's printf functionality is split in two steps:
* format the data into a string with format (which is very much like sprintf)
* output the resulting string with puts. For example,
To process a text file line by line, you have two choices. If the file is smaller than several megabytes, you can read it just in one go:
For files of any big size, you can read the lines one by one (though this is slightly slower than the above approach):
Finally, if you can format your file so that it is executable Tcl code, the following reading method is fastest:
To "touch a file", i.e. create it if not exists, and in any case update its modification time, you can use this:

### "Binary" files

All files are made of bytes, which are made of bits, which are binary. The term "binary" with files relates mostly to the fact that they can contain bytes of any value, and line-ends (Carriage Return+Newline in the DOS/Windows world) are not to be translated. Tcl can handle "binary" files without a problem -- just configure the channel as binary after opening:
Now the variable content holds the file's contents, byte for byte.
To test whether a file is "binary", in the sense that it contains NUL bytes:

### The file command

Many useful operations with files are collected in the file command. The first argument tells which operation to do:
* file atime name ?time?
* file attributes name
* file attributes name ?option?
* file attributes name ?option value option value...?
* file channels ?pattern? - returns the handles of currently open files
* file copy ?-force? ?- -? source target
* file copy ?-force? ?- -? source ?source ...? targetDir
* file delete ?-force? ?- -? pathname ?pathname ... ?
* file dirname name - e.g. [file dirname /foo/bar/grill.txt] -> /foo/bar
* file executable name
* file exists name
* file extension name - e.g. [file extension /foo/bar/grill.txt] -> .txt
* file isdirectory name
* file isfile name
* file join name ?name ...?
* file link ?-linktype? linkName ?target?
* file lstat name varName
* file mkdir dir ?dir ...? - creates one or more directories (folders)
* file mtime name ?time?
* file nativename name
* file normalize name
* file owned name
* file pathtype name
* file readable name
* file readlink name
* file rename ?-force? ?- -? source target
* file rename ?-force? ?- -? source ?source ...? targetDir
* file rootname name - e.g. [file rootname /foo/bar/grill.txt] -> /foo/bar/grill
* file separator ?name?
* file size name
* file split name - e.g [file split /foo/bar/grill.txt] -> {foo bar grill.txt}
* file stat name varName
* file system name
* file tail name - e.g. [file tail /foo/bar/grill.txt] -> grill.txt
* file type name
* file volumes - Windows: returns your "drive letters", e.g {A:/ C:/}
* file writable name

## Tcl examples

Most of these example scripts first appeared in the Tclers' Wiki http://wiki.tcl.tk . The author (Richard Suchenwirth) declares them to be fully in the public domain. The following scripts are plain Tcl, they don't use the Tk GUI toolkit (there's a separate chapter for those).

### Sets as lists

Tcl's lists are well suited to represent sets. Here's typical set operations. If you use the tiny testing framework explained earlier, the e.g. lines make the self-test; otherwise they just illustrate how the operations should work.

### Hex-dumping a file

The following example code opens a file, configures it to binary translation (i.e. line-ends \r\n are not standardized to \n as usual in C), and prints as many lines as needed which each contain 16 bytes in hexadecimal notation, plus, where possible, the ASCII character.
The "main routine" is a single line that dumps all files given on the command line:
Sample output, the script applied to itself:

### Roman numerals

Roman numerals are an additive (and partially subtractive) system with the following letter values:
Here's some Tcl routines for dealing with Roman numerals.
Sorting roman numerals: I,V,X already come in the right order; for the others we have to introduce temporary collation transformations, which we'll undo right after sorting:
Roman numerals from integer:
Roman numerals parsed into integer:

### Custom control structures

As "control structures" are really nothing special in Tcl, just a set of commands, it is easier than in most other languages to create one's own. For instance, if you would like to simplify the for loop
for the typical simple cases so you can write instead
here is an implementation that even returns a list of the results of each iteration:
using this, a string reverse function can be had as a one-liner:

#### Range-aware switch

Another example is the following range-aware switch variation. A range (numeric or strings) can be given as from..to, and the associated scriptlet gets executed if the tested value lies inside that range.
Like in switch, fall-through collapsing of several cases is indicated by "-", and "default" as final condition fires if none else did. Different from switch, numbers are compared by numeric value, no matter whether given as decimal, octal or hex.
Testing:

#### The K combinator

A very simple control structure (one might also call it a result dispatcher) is the K combinator, which is almost terribly simple:
It can be used in all situations where you want to deliver a result that is not the last. For instance, reading a file in one go:
can be simplified, without need for the data variable, to:
Another example, popping a stack:
This is in some ways similar to LISP's PROG1 construct: evaluate the contained expressions, and return the result of the first one.

### Rational numbers

Rational numbers, a.k.a. fractions, can be thought of as pairs of integers {numerator denominator}, such that their "real" numerical value is numerator/denominator (and not in integer nor "double" division!). They can be more precise than any "float" or "double" numbers on computers, as those can't exactly represent any fractions whose denominator isn't a power of 2 — consider $^1$⁄$_3$ which can not at any precision be exactly represented as floating-point number to base 2, nor as decimal fraction (base 10), even if bignum.
An obvious string representation of a rational is of course "n/d". The following "constructor" does that, plus it normalizes the signs, reduces to lowest terms, and returns just the integer n if d==1:
Conversely, this "deconstructor" splits zero or more rational or integer strings into num and den variables, such that [ratsplit 1/3 a b] assigns 1 to a and 3 to b:
Arithmetical helper functions can be wrapped with func if they only consist of one call of expr:

### Docstrings

Languages like Lisp and Python have the docstring feature, where a string in the beginning of a function can be retrieved for on-line (or printed) documentation. Tcl doesn't have this mechanism built-in (and it would be hard to do it exactly the same way, because everything is a string), but a similar mechanism can easily be adopted, and it doesn't look bad in comparison:
* Common Lisp: (documentation 'foo 'function)
* Python: foo.__doc__
* Tcl: docstring foo
If the docstring is written in comments at the top of a proc body, it is easy to parse it out. In addition, for all procs, even without docstring, you get the "signature" (proc name and arguments with defaults). The code below also serves as usage example: }
Testing:

### Factorial

Factorial (n!) is a popular function with super-exponential growth. Mathematically put,
In Tcl, we can have it pretty similarly:
But this very soon crosses the limits of integers, giving wrong results.
A math book showed me the Stirling approximation to n! for large n (at Tcl's precisions, "large" is > 20 ...), so I built that in:
Just in case somebody needs approximated large factorials... But for n>143 we reach the domain limit of floating point numbers. In fact, the float limit is at n>170, so an intermediate result in the Stirling formula must have busted at 144. For such few values it is most efficient to just look them up in a pre-built table, as Tcllib's math::factorial does.

### How big is A4?

Letter and Legal paper formats are popular in the US and other places. In Europe and elsewhere, the most widely used paper format is called A4. To find out how big a paper format is, one can measure an instance with a ruler, or look up appropriate documentation. The A formats can also be deduced from the following axioms:
* A0 has an area of one square meter
* A(n) has half the area of A(n-1)
* The ratio between the longer and the shorter side of an A format is constant
How much this ratio is, can easily be computed if we consider that A(n) is produced from A(n-1) by halving it parallel to the shorter side, so
So here is my Tcl implementation, which returns a list of height and width in centimeters (10000 cm$^2$ = 1 m$^2$) with two fractional digits, which delivers a sufficient precision of 1/10 mm: }

### Bit vectors

Here is a routine for querying or setting single bits in vectors, where bits are addressed by non-negative integers. Implementation is as a "little-endian" list of integers, where bits 0..31 are in the first list element, 32..63 in the second, etc.
Usage: bit varName position ?bitval?
If bitval is given, sets the bit at numeric position position to 1 if bitval != 0, else to 0; in any case returns the bit value at specified position. If variable varName does not exist in caller's scope, it will be created; if it is not long enough, it will be extended to hold at least $position+1 bits, e.g. bit foo 32 will turn foo into a list of two integers, if it was only one before. All bits are initialized to 0.
This may be used for Boolean properties of numerically indexed sets of items. Example: An existence map of ZIP codes between 00000 and 99999 can be kept in a list of 3125 integers (where each element requires about 15 bytes overall), while implementing the map as an array would take 100000 * 42 bytes in worst case, but still more than a bit vector if the population isn't extremely sparse — in that case, a list of 1-bit positions, retrieved with lsearch, might be more efficient in memory usage. Runtime of bit vector accesses is constant, except when a vector has to be extended to much larger length.
Bit vectors can also be used to indicate set membership (set operations would run faster if processing 32 bits on one go with bitwise operators (&, |, ~, ^)) — or pixels in a binary imary image, where each row could be implemented by a bitvector.
Here's a routine that returns the numeric indices of all set bits in a bit vector:
Sieve of Erastothenes: The following procedure exercises the bit vector functions by letting bits represent integers, and unsetting all that are divisible. The numbers of the bits finally still set are supposed to be primes, and returned:
Here's code to count the number of 1-bits in a bit vector, represented as an integer list. It does so by adding the values of the hex digits:

### Stacks and queues

Stacks and queues are containers for data objects with typical access methods:
* push: add one object to the container
* pop: retrieve and remove one object from the container
In Tcl it is easiest to implement stacks and queues with lists, and the push method is most naturally lappend, so we only have to code a single generic line for all stacks and queues:
It is pop operations in which stacks, queues, and priority queues differ:
* in a stack, the most recently pushed object is retrieved and removed (last in first out, LIFO)
* in a (normal) queue, it is the least recently pushed object (first in first out, FIFO)
* in a priority queue, the object with the highest priority comes first.
Priority (a number) has to be assigned at pushing time — by pushing a list of two elements, the item itself and the priority, e.g..
In a frequent parlage, priority 1 is the "highest", and the number increases for "lower" priorities — but you could push in an item with 0 for "ultrahigh" ;-) Popping a stack can be done like this:
Popping a queue is similarly structured, but with so different details that I found no convenient way to factor out things:
Popping a priority queue requires sorting out which item has highest priority. Sorting can be done when pushing, or when popping, and since our push is so nicely generic I prefer the second choice (as the number of pushs and pops should be about equal, it does not really matter). Tcl's lsort is stable, so items with equal priority will remain in the order in which they were queued:
A practical application is e.g. in state space searching, where the kind of container of the to-do list determines the strategy:
* stack is depth-first
* (normal) queue is breadth-first
* priority queue is any of the more clever ways: A*, Greedy, ...
Recent-use lists: A variation that can be used both in a stack or queue fashion is a list of values in order of their last use (which may come handy in an editor to display the last edited files, for instance). Here, pushing has to be done by dedicated code because a previous instance would have to be removed:
The first element is the least recently, the last the most recently used. Elements are not removed by the popping, but (if necessary) when re-pushing. (One might truncate the list at front if it gets too long).

### Functions

Functions in Tcl are typically written with the proc command. But I notice more and more that, on my way to functional programming, my proc bodies are a single call to expr which does all the rest (often with the powerful x?y:z operator). So what about a thin abstraction (wrapper) around this recurring pattern?
(I might have called it fun as well... it sure is.) That's all. A collateral advantage is that all expressions are braced, without me having to care. But to not make the page look so empty, here's some examples for func uses:
Pity we have to make expr explicit again, in nested calls like in gcd ... But func isn't limited to math functions (which, especially when recursive, come out nice), but for expr uses in testing predicates as well:
Exposing expr binary arithmetic operators as Tcl commands goes quite easy too:
For "-", we distinguish unary and binary form:
Having the modulo operator exposed, gcd now looks nicer:
For unary not I prefer that name to "!", as it might also stand for factorial — and see the shortest function body I ever wrote :^) :
Without big mention, functions implemented by recursion have a pattern for which func is well suited (see fac and gcd above). Another example is this integer range generator (starts from 1, and is inclusive, so [iota1 5] == {1 2 3 4 5}):

### Experiments with Boolean functions

"NAND is not AND." Here are some Tcl codelets to demonstrate how all Boolean operations can be expressed in terms of the single NAND operator, which returns true if not both his two inputs are true (NOR would have done equally well). We have Boolean operators in expr, so here goes:
The only unary operator NOT can be written in terms of nand:
.. and everything else can be built from them too:
Here's some testing tools — to see whether an implementation is correct, look at its truth table, here done as the four results for A,B combinations 0,0 0,1 1,0 1,1 — side note: observe how easily functions can be passed in as arguments:
To see how efficient the implementation is (in terms of NAND units used), try this, which relies on the fact that Boolean functions contain no lowercase letters apart from the operator names:
As a very different idea, having nothing to do with NAND as elementary function, the following generic code "implements" Boolean functions very intuitively, by just giving their truth table for look-up at runtime:

### Solving cryptarithms

Cryptarithms are puzzles where digits are represented by letters, and the task is to find out which. The following "General Problem Solver" (for small values of General) uses heavy metaprogramming: it
* builds up a nest of foreachs suiting the problem,
* quick kills (with continue) to force unique values for the variables, and
* returns the first solution found, or else an empty string:
This works fine on some well-known cryptarithms:

### Database experiments
#### A simple array-based database

There are lots of complex databases around. Here I want to explore how a database can be implemented in the Tcl spirit of simplicity, and how far that approach takes us. Consider the following model:
* A database is a set of records
* A record is a nonempty set of fields with a unique ID
* A field is a pair of tag and nonempty value, both being strings
Fields may well be implemented as array entries, so we could have an array per record, or better one array for the whole database, where the key is composed of ID and tag. Unique IDs can be had by just counting up (incrementing the highest ID so far). The process of creating a simple database consists only of setting an initial value for the ID:
Let's consider a library application for an example. Adding a book to the database can be simply done by
Note that, as we never specified what fields a record shall contain, we can add whatever we see fit. For easier handling, it's a good idea to classify records somehow (we'll want to store more than books), so we add
Retrieving a record is as easy as this (though the fields come in undefined order):
and deleting a record is only slightly more convolved:
or, even easier and faster from Tcl 8.3 on:
Here's how to get a "column", all fields of a given tag:
But real columns may have empty fields, which we don't want to store. Retrieving fields that may not physically exist needs a tolerant access function:
In a classical database we have to define tables: which fields of what type and of which width. Here we can do what we want, even retrieve which fields we have used so far (using a temporary array to keep track of field names):
Searching for records that meet a certain condition can be done sequentially. For instance, we want all books printed before 1980:
We might also store our patrons in the same database (here in a different style):
Without a concept of "tables", we can now introduce structures like in relational databases. Assume John Smith borrows "The Tempest". We have the patron's and book's ID in variables and do double bookkeeping:
When he returns the book, the process is reversed:
The dueback field (%Y-%M-%d format is good for sorting and comparing) is useful for checking whether books have not been returned in time:
Likewise, parts of the accounting (e.g. orders to, and bills from, booksellers) can be added with little effort, and cross-related also to external files (just set the value to the filename).
Indexes: As shown, we can retrieve all data by sequential searching over array names. But if the database grows in size, it's a good idea to create indexes which cross-reference tags and values to IDs. For instance, here's how to make an authors' index in four lines:
gives us a books list of all authors matching the given glob pattern (we reuse Tcl's functionality, instead of reinventing it...). Indexes are useful for repeated information that is likely to be searched. Especially, indexing the isa field allows iterating over "tables" (which we still don't explicitly have!;-):
And beyond industry-standard SQL, we can search multiple indices in one query:
gives you all (case-independent) occurrences of MARK, be it in patron's names, book's authors or titles. As versatile as good old grep...
Persistence: Databases are supposed to exist between sessions, so here's how to save a database to a file:
and loading a database is even easier (on re-loading, better unset the array before):
If you use characters outside your system encoding (no problem to write Japanese book titles in Kanji), you'll have to fconfigure (e.g -encoding utf-8) on saving and loading, but that's just a few more LOC. Saving also goes a good way to what is ceremonially called "committing" (you'll need write-locking for multi-user systems), while loading (without saving before) might be called a "one-level rollback", where you want to discard your latest changes.
Notice that so far we have only defined one short proc, all other operations were done with built-in Tcl commands only. For clearer code, it is advisable to factor out frequent operations into procs, e.g.
Of course, with growing databases we may reach memory limits: arrays need some extra storage for administration. On the other hand, the present approach is pretty economic, since it does not use field widths (all strings are "shrink-wrapped"), and omits empty fields, while at the same time allowing to add whatever fields you wish. A further optimization could be to tally value strings, and replace the frequent ones with "@$id", where db(@$id) holds the value once, and only db'get has to be adapted to redirect the query.
Also, memory limits on modern computers are somewhere up high... so only at some time in the future you might have (but maybe not want) to change to a complex database ;-)
On the limits: Tcl arrays may get quite large (one app was reported to store 800000 keys in Greek characters), and at some point enumerating all keys with array names db (which produces one long list) may exceed your available memory, causing the process to swap. In that situation, you can fall back to the (otherwise slower, and uglier) use of a dedicated iterator:
But neither can you filter the keys you will get with a glob pattern, nor may you add or delete array elements in the loop — the search will be immediately terminated.

#### Tables as lists of lists

Tables are understood here as rectangular (matrix) arrangements of data in rows (one row per "item"/"record") and columns (one column per "field"/"element"). They are for instance the building blocks of relational databases and spreadsheets. In Tcl, a sensible implementation for compact data storage would be as a list of lists. This way, they are "pure values" and can be passed e.g. through functions that take a table and return a table. No con-/destructors are needed, in contrast to the heavierweight matrix in Tcllib. I know there are many table implementations in Tcl, but like so often I wanted to build one "with my bare hands" and as simple as possible. As you see below, many functionalities can be "implemented" by just using Tcl's list functions.
A nice table also has a header line, that specifies the field names. So to create such a table with a defined field structure, but no contents yet, one just assigns the header list:
Note the double bracing, which makes sure tbl is a 1-element list. Adding "records" to the table is as easy as
Make sure the fields (cells) match those in the header. Here single bracing is correct. If a field content contains spaces, it must be quoted or braced too:
Sorting a table can be done with lsort -index, taking care that the header line stays on top:
Removing a row (or contiguous sequence of rows) by numeric index is a job for lreplace:
Simple printing of such a table, a row per line, is easy with
Accessing fields in a table is more fun with the field names than the numeric indexes, which is made easy by the fact that the field names are in the first row:
You can then access cells:
and replace cell contents like this:
Here is how to filter a table by giving pairs of field name and glob-style expression — in addition to the header line, all rows that satisfy at least one of those come through (you can force AND behavior by just nesting such calls):
This filters (and, if wanted, rearranges) columns, sort of what is called a "view":

### Programming Languages Laboratory

In the following few chapters you'll see how easy it is to emulate or explore other programming languages with Tcl.

#### GOTO: a little state machine

The GOTO "jumping" instruction is considered harmful in programming for many years now, but still it might be interesting to experiment with. Tcl has no goto command, but it can easily be created. The following code was created in the Tcl chatroom, instigated by the quote: "A computer is a state machine. Threads are for people who can't program state machines."
So here is one model of a state machine in ten lines of code. The "machine" itself takes a list of alternating labels and state code; if a state code does not end in a goto or break, the same state will be repeated as long as not left, with goto or break (implicit endless loop). The goto command is defined "locally", and deleted after leaving the state machine — it is not meaningfully used outside of it. Execution starts at the first of the states.
Testing: a tiny state machine that greets you as often as you wish, and ends if you only hit Return on the "how often?" question:

#### Playing Assembler

In this weekend fun project to emulate machine language, I picked those parts of Intel 8080A/8085 Assembler (because I had a detailed reference handy) that are easily implemented and still somehow educational (or nostalgic ;-).
Of course this is no real assembler. The memory model is constant-size instructions (strings in array elements), which are implemented as Tcl procs. So an "assembler" program in this plaything will run even slower than in pure Tcl, and consume more memory — while normally you associate speed and conciseness with "real" assembler code. But it looks halfway like the real thing: you get sort of an assembly listing with symbol table, and can run it — I'd hardly start writing an assembler in C, but in Tcl it's fun for a sunny Sunday afternoon... }
Now testing:
The mov b,INCR part is an oversimplification. For a real 8080, one would have to say
Since the pseudo-register M can also be used for writing back, it cannot be implemented by simply copying the value. Rather, one could use read and write traces on variable M, causing it to load from, or store to, mem($HL). Maybe another weekend...

#### Functional programming (Backus 1977)

John Backus turned 80 these days. For creating FORTRAN and the BNF style of language description, he received the ACM Turing Award in 1977. In his Turing Award lecture,
Can Programming Be Liberated from the von Neumann Style? A Functional Style and Its Algebra of Programs. (Comm. ACM 21.8, Aug. 1978, 613-641)
he developed an amazing framework for functional programming, from theoretical foundations to implementation hints, e.g. for installation, user privileges, and system self-protection. In a nutshell, his FP system comprises
* a set O of objects (atoms or sequences)
* a set F of functions that map objects into objects (f : O |-> O}
* an operation, application (very roughly, eval)
* a set FF of functional forms, used to combine functions or objects to form new functions in F
* a set D of definitions that map names to functions in F
I'm far from having digested it all, but like so often, interesting reading prompts me to do Tcl experiments, especially on weekends. I started with Backus' first Functional Program example,
and wanted to bring it to life — slightly adapted to Tcl style, especially by replacing the infix operator "o" with a Polish prefix style:
Unlike procs or lambdas, more like APL or RPN, this definition needs no variables — it declares (from right to left) what to do with the input; the result of each step is the input for the next step (to the left of it). In an RPN language, the example might look like this:
which has the advantage that execution goes from left to right, but requires some stack awareness (and some swaps to set the stack right ;^)
Implementing Def, I took an easy route by just creating a proc that adds an argument and leaves it to the "functional" to do the right thing (with some quoting heaven :-) }
For functional composition, where, say for two functions f and g,
again a proc is created that does the bracket nesting:
Why Backus used Transpose on the input, wasn't first clear to me, but as he (like we Tclers) represents a matrix as a list of rows, which are again lists (also known as vectors), it later made much sense to me. This code for transposing a matrix uses the fact that variable names can be any string, including those that look like integers, so the column contents are collected into variables named 0 1 2 ... and finally turned into the result list:
An integer range generator produces the variable names, e.g iota 3 => {0 1 2}
...and Insert is better known as fold, I suppose. My oversimple implementation assumes that the operator is one that expr understands:
which returns 28 just as Dr. Backus ordered (= 1*6 + 2*5 + 3*4). Ah, the joys of weekend Tcl'ing... — and belatedly, Happy Birthday, John! :)
Another example, cooked up by myself this time, computes the average of a list. For this we need to implement the construction operator, which is sort of inverse mapping — while mapping a function over a sequence of inputs produces a sequence of outputs of that function applied to each input, Backus' construction maps a sequence of functions over one input to produce a sequence of results of each function to that input, e.g.
Of course I can't use circumfix brackets as operator name, so let's call it constr:
which returns correctly 3. However, as integer division takes place, it would be better to make that
giving the correct result 2.5. However, the auxiliary definition for dlength cannot be inlined into the definition of mean — so this needs more work... But this version, that maps double first, works:
One more experiment, just to get the feel:
which gives 5.0. Compared to an RPN language, hypot would be
which is shorter and simpler, but meddles more directly with the stack.
An important functional form is the conditional, which at Backus looks like
meaning, translated to Tcl,
Let's try that, rewritten Polish-ly to:

#### Reusable functional components

Say you want to make a multiplication table for an elementary school kid near you. Easily done in a few lines of Tcl code:
The code does not directly puts its results, but returns them as a string — you might want to do other things with it, e.g. save it to a file for printing. Testing:
Or print the result directly from wish:
Here's a different way to do it à la functional programming:
The body is nice and short, but consists of all unfamiliar commands. They are however better reusable than the multable proc above. The first formats a matrix (a list of lists to Tcl) with newlines and aligned columns for better display:
Short again, and slightly cryptic, as is the "outer product" routine, which takes a function f and two vectors, and produces a matrix where f was applied to every pair of a x b — in APL they had special compound operators for this job, in this case "°.x":
Again, lmap (the collecting foreach) figures prominently, so here it is in all its simplicity:
With these parts in place, we can see that multable2 works as we want:
So why write six procedures, where one did the job already? A matter of style and taste, in a way — multable is 10 LOC and depends on nothing but Tcl, which is good; multable2 describes quite concisely what it does, and builds on a few other procs that are highly reusable.
Should you need a unit matrix (where the main diagonal is 1, and the rest is 0), just call outProd with a different function (equality, ==):
which just requires expr's equality to be exposed too:
One of the fascinations of functional programming is that one can do the job in a simple and clear way (typically a one-liner), while using a collection of reusable building-blocks like lmap and iota. And formatMatrix and outProd are so general that one might include them in some library, while the task of producing a multiplication table may not come up any more for a long time...

#### Modelling an RPN language

Tcl follows strictly the Polish notation, where an operator or function always precedes its arguments. It is however easy to build an interpreter for a language in Reverse Polish Notation (RPN) like Forth, Postscript, or Joy, and experiment with it.
The "runtime engine" is just called "r" (not to be confused with the R language), and it boils down to a three-way switch done for each word, in eleven lines of code:
* "tcl" evaluates the top of stack as a Tcl script
* known words in the ::C array are recursively evaluated in "r"
* other words are just pushed
Joy's rich quoting for types ([list], {set}, "string", 'char) conflict with the Tcl parser, so lists in "r" are {braced} if their length isn't 1, and (parenthesized) if it is — but the word shall not be evaluated now. This looks better to me than /slashing as in Postscript.
As everything is a string, and to Tcl "a" is {a} is a , Joy's polymorphy has to be made explicit. I added converters between characters and integers, and between strings and lists (see the dictionary below). For Joy's sets I haven't bothered yet — they are restricted to the domain 0..31, probably implemented with bits in a 32-bit word.
Far as this is from Joy, it was mostly triggered by the examples in Manfred von Thun's papers, so I tongue-in-cheek still call it "Pocket Joy" — it was for me, at last, on the iPaq... The test suite at end should give many examples of what one can do in "r". }
# That's it. Stack (list) and Command array are global variables:
#-- A tiny switchable debugger:
Definitions are in Forth style — ":" as initial word, as they look much more compact than Joy's DEFINE n == args;
expr functionality is exposed for binary operators and one-arg functions:
Helper functions written in Tcl:

#### Tacit programming

The J programming language is the "blessed successor" to APL, where "every function is an infix or prefix operator", x?y (dyadic) or ?y (monadic), for ? being any pre- or user-defined function).
"Tacit programming" (tacit: implied; indicated by necessary connotation though not expressed directly) is one of the styles possible in J, and means coding by combining functions, without reference to argument names. This idea may have been first brought up in Functional programming (Backus 1977), if not in Forth and Joy, and it's an interesting simplification compared to the lambda calculus.
For instance, here's a breathtakingly short J program to compute the mean of a list of numbers:
Let's chew this, byte by byte :)
Only implicitly present is a powerful function combinator called "fork". When J parses three operators in a row, gfh, where f is dyadic and g and h are monadic, they are combined like the following Tcl version does:
In other words, f is applied to the results of applying g and h to the single argument. Note that +/ is considered one operator, which applies the "adverb" folding to the "verb" addition (one might well call it "sum"). When two operands occur together, the "hook" pattern is implied, which might in Tcl be written as:
As KBK pointed out in the Tcl chatroom, the "hook" pattern corresponds to Schönfinkel/Curry's S combinator (see Hot Curry and Combinator Engine), while "fork" is called S' there.
Unlike in earlier years when I was playing APL, this time my aim was not to parse and emulate J in Tcl — I expected hard work for a dubitable gain, and this is a weekend fun project after all. I rather wanted to explore some of these concepts and how to use them in Tcl, so that in slightly more verbose words I could code (and call)
following Backus' FP language with the "Def" command. So let's get the pieces together. My "Def" creates an interp alias, which is a good and simple Tcl way to compose partial scripts (the definition, here) with one or more arguments, also known as "currying":
The second parameter, "=", is for better looks only and evidently never used.
Testing early and often is a virtue, as is documentation — to make the following code snippets clearer, I tuned my little tester for better looks, so that the test cases in the source code also serve as well readable examples — they look like comments but are code! The cute name "e.g." was instigated by the fact that in J, "NB." is used as comment indicator, both being well known Latin abbreviations:
Again, the "->" argument is for eye-candy only — but it feels better to me at least. See the examples soon to come.
For recursive functions and other arithmetics, func makes better reading, by accepting expr language in the body:
We'll use this to turn expr's infix operators into dyadic functions, plus the "slashdot" operator that makes division always return a real number, hence the dot :
For "fold", this time I devised a recursive version:
Tacit enough (one might have picked fancier names like +/ for "sum" and # as alias for llength), but in principle it is equivalent to the J version, and doesn't name a single argument. Also, the use of llength demonstrates that any good old Tcl command can go in here, not just the artificial Tacit world that I'm just creating...
In the next step, I want to reimplement the "median" function, which for a sorted list returns the central element if its length is odd, or the mean of the two elements adjacent to the (virtual) center for even length. In J, it looks like this:
which may better explain why I wouldn't want to code in J :^) J has ASCIIfied the zoo of APL strange character operators, at the cost of using braces and brackets as operators too, without regard for balancing, and extending them with dots and colons, so e.g.
J code sometimes really looks like an accident in a keyboard factory... I won't go into all details of the above code, just some:
(<.,>.) is building a list of the floor and the ceiling of its single argument, the comma being the concatenation operator here, comparable to Backus' "construction" or Joy's cleave. The pattern
is a kind of conditional in J, which could in Tcl be written
but my variant of the median algorithm doesn't need a conditional — for lists of odd length it just uses the central index twice, which is idempotent for "mean", even if a tad slower.
J's "from" operator { takes zero or more elements from a list, possibly repeatedly. For porting this, lmap is a good helper, even though not strictly functional:
We furtheron borrow some more content from expr:
We'll need functional composition, and here's a recursive de-luxe version that takes zero or more functions, hence the name o*:
Evidently, identity as could be written
is the neutral element of variadic functional composition, when called with no functions at all.
If composite functions like 'fork' are arguments to o*, we'd better let unknown know that we want auto-expansion of first word:
Also, we need a numeric sort that's good for integers as well as reals ("Def" serves for all kinds of aliases, not just combinations of functions):
As this file gets tacitly sourced, I am pretty confident that I've reached my goal for this weekend — even though my median doesn't remotely look like the J version: it is as "wordy" as Tcl usually is. But the admittedly still very trivial challenge was met in truly function-level style, concerning the definitions of median, center and mean — no variable left behind. And that is one, and not the worst, Tcl way of Tacit programming...

### Vector arithmetics

APL and J (see Tacit programming) have the feature that arithmetics can be done with vectors and arrays as well as scalar numbers, in the varieties (for any operator @):
* scalar @ scalar → scalar (like expr does)
* vector @ scalar → vector
* scalar @ vector → vector
* vector @ vector → vector (all of same dimensions, element-wise)
Here's experiments how to do this in Tcl. First lmap is a collecting foreach — it maps the specified body over a list:
The following generic wrapper takes one binary operator (could be any suitable function) and two arguments, which may be scalars, vectors, or even matrices (lists of lists), as it recurses as often as needed. Note that as my lmap above only takes one list, the two-list case had to be made explicit with foreach.
Tests are done with this minimal "framework":
Scalar + Scalar
Scalar + Vector
Vector / Scalar
Vector + Vector
Matrix * Scalar
Multiplying a 3x3 matrix with another:
The dot product of two vectors is a scalar. That's easily had too, given a sum function:
should result in 11 (= (1*3)+(2*4)).
Here's a little application for this: a vector factorizer, that produces the list of divisors for a given integer. For this we again need a 1-based integer range generator:
At this point, a number is prime if the sum of the latest vector is 2. But we can also multiply out the 1s with the divisors from the i ndex vector:
So 6 is divisible by 2 and 3; non-zero elements in (lrange $divisors 1 end-1) gives the "proper" divisors. And three nested calls to vec are sufficient to produce the divisors list :)
Just for comparison, here's how it looks in J:

### Integers as Boolean functions

Boolean functions, in which arguments and result are in the domain {true, false}, or {1, 0} as expr has it, and operators are e.g. {AND, OR, NOT} resp. {&&, ||, !}, can be represented by their truth table, which for example for {$a && $b} looks like:
As all but the last column just enumerate all possible combinations of the arguments, first column least-significant, the full representation of a&&b is the last column, a sequence of 0s and 1s which can be seen as binary integer, reading from bottom up: 1 0 0 0 == 8. So 8 is the associated integer of a&&b, but not only of this — we get the same integer for !(!a || !b), but then again, these functions are equivalent.
To try this in Tcl, here's a truth table generator that I borrowed from a little proving engine, but without the lsort used there — the order of cases delivered makes best sense when the first bit is least significant: }
Now we can write n(f), which, given a Boolean function of one or more arguments, returns its characteristic number, by iterating over all cases in the truth table, and setting a bit where appropriate:
Experimenting:
So the characteristic integer is not the same as the Goedel number of a function, which would encode the structure of operators used there.
Getting more daring, let's try a distributive law:
Daring more: what if we postulate the equivalence?
Without proof, I just claim that every function of n arguments whose characteristic integer is 2^(2^n) — 1 is a tautology (or a true statement — all bits are 1). Conversely, postulating non-equivalence turns out to be false in all cases, hence a contradiction:
So again, we have a little proving engine, and simpler than last time.
In the opposite direction, we can call a Boolean function by its number and provide one or more arguments — if we give more than the function can make sense of, non-false excess arguments lead to constant falsity, as the integer can be considered zero-extended:
Trying again, starting at OR (14):
So f(n) 14 indeed behaves like the OR function — little surprise, as its truth table (the results of the four calls), read bottom-up, 1110, is decimal 14 (8 + 4 + 2). Another test, inequality:
Trying to call 14 (OR) with more than two args:
The constant 0 result is a subtle indication that we did something wrong :)
Implication (if a then b, a -> b) can in expr be expressed as $a <= $b — just note that the "arrow" seems to point the wrong way. Let's try to prove "Modus Barbara" — "if a implies b and b implies c, then a implies c":
With less abstract variable names, one might as well write
But this has been verified long ago, by Socrates' death :^)

### Let unknown know

To extend Tcl, i.e. to make it understand and do things that before raised an error, the easiest way is to write a proc. Any proc must however be called in compliance with Tcl's fundamental syntax: first word is the command name, then the arguments separated by whitespace. Deeper changes are possible with the unknown command, which is called if a command name is, well, unknown, and in the standard version tries to call executables, to auto-load scripts, or do other helpful things (see the file init.tcl). One could edit that file (not recommended), or rename unknown to something else and provide one's own unknown handler, that falls through to the original proc if unsuccessful, as shown in Radical language modification.
Here is a simpler way that allows to extend unknown "in place" and incrementally: We let unknown "know" what action it shall take under what conditions. The know command is called with a condition that should result in an integer when given to expr, and a body that will be executed if cond results in nonzero, returning the last result if not terminated with an explicit return. In both cond and body you may use the variable args that holds the problem command unknown was invoked with.
The extending code what is prepended to the previous unknown body. This means that subsequent calls to know stack up, last condition being tried first, so if you have several conditions that fire on the same input, let them be "known" from generic to specific.
Here's a little debugging helper, to find out why "know" conditions don't fire:
Now testing what new magic this handful of code allows us to do. This simple example invokes expr if the "command" is digestible for it:

### If we had no if

Imagine the makers of Tcl had failed to provide the if command. All the rest would be there. Doing more steps towards functional programming, I came upon this interesting problem, and will shortly demonstrate that it can easily be solved in pure-Tcl.
We still have the canonical truth values 0 and 1 as returned from expr with a comparison operator. The idea in the paper I read is to use them as names of very simple functions:
Glory be to the 11 rules of man Tcl that this is already a crude though sufficient reimplementation:
The bracketed expr command is evaluated first, returning 0 or 1 as result of the comparison. This result (0 or 1) is substituted for the first word of this command. The other words (arguments) are not substituted because they're curly-braced, so either 0 or 1 is invoked, and does its simple job. (I used uplevel instead of eval to keep all side effects in caller's scope). Formally, what happened to the bracketed call is that it went through "applicative order" evaluation (i.e., do it now), while the braced commands wait for "normal order" evaluation (i.e., do when needed, maybe never — the need is expressed through eval/upvar or similar commands).
Though slick at first sight, we actually have to type more. As a second step, we create the If command that wraps the expr invocation:
This again passes impromptu tests, and adds the feature that any non-zero value counts as true and returns 1 — if we neglect the other syntactic options of if, especially the elseif chaining. However, this is no fundamental problem — consider that
can be rewritten as
so the two-way If is about as mighty as the real thing, give or take a few braces and redundant keywords (then, else).
Luckily we have an if in Tcl (and it certainly fares better in byte-code compilation), but on leisurely evenings it's not the microseconds that count (for me at least) — it's rather reading on the most surprising (or fundamental) ideas, and demonstrating how easily Tcl can bring them to life...

### Brute force meets Goedel

Never afraid of anything (as long as everything is a string), a discussion in the Tcl chatroom brought me to try the following: let the computer write ("discover") its own software, only given specifications of input and output. In truly brute force, up to half a million programs are automatically written and (a suitable subset of them) tested to find the one that passes the tests.
To make things easier, this flavor of "software" is in a very simple RPN language similar to, but much smaller than, the one presented in Playing bytecode: stack-oriented like Forth, each operation being one byte (ASCII char) wide, so we don't even need whitespace in between. Arguments are pushed on the stack, and the result of the "software", the stack at end, is returned. For example, in
execution of the script "++" should sum its three arguments (1+(2+3)), and return 6.
Here's the "bytecode engine" (ebc: execute byte code), which retrieves the implementations of bytecodes from the global array cmd:
Let's now populate the bytecode collection. The set of all defined bytecodes will be the alphabet of this little RPN language. It may be interesting to note that this language has truly minimal syntax — the only rule is: each script ("word") composed of any number of bytecodes is well-formed. It just remains to check whether it does what we want.
Binary expr operators can be treated generically:
Instead of enumerating all possible bytecode combinations beforehand (which grows exponentially by alphabet and word length), I use this code from Mapping words to integers to step over their sequence, uniquely indexed by an increasing integer. This is something like the Goedel number of the corresponding code. Note that with this mapping, all valid programs (bytecode sequences) correspond to one unique non-negative integer, and longer programs have higher integers associated:
Now out for discovery! The toplevel proc takes a paired list of inputs and expected output. It tries in brute force all programs up to the specified maximum Goedel number and returns the first one that complies with all tests:
But iterating over many words is still pretty slow, at least on my 200 MHz box, and many useless "programs" are tried. For instance, if the test has two inputs and wants one output, the stack balance is -1 (one less out than in). This is provided e.g. by one the binary operators +-*/. But the program "dd" (which just duplicates the top of stack twice) has a stack balance of +2, and hence can never pass the example test. So, on a morning dogwalk, I thought out this strategy:
* measure the stack balance for each bytecode
* iterate once over very many possible programs, computing their stack balance
* partition them (put into distinct subsets) by stack balance
* perform each 'discovery' call only on programs of matching stack balance
Here's this version. Single bytecodes are executed, only to measure their effect on the stack. The balance of longer programs can be computed by just adding the balances of their individual bytecodes:
The partitioning will run for some seconds (depending on nmax — I tried with several ten thousand), but it's needed only once. The size of partitions is further reduced by excluding programs which contain redundant code, that will have no effect, like swapping the stack twice, or swapping before an addition or multiplication. A program without such extravaganzas is shorter and yet does the same job, so it will have been tested earlier anyway.
The discoverer, Second Edition, determines the stack balance of the first text, and tests only those programs of the same partition:
But now for the trying. The partitioning helps very much in reducing the number of candidates. For the 1000 programs with Goedel numbers 1..1000, it retains only a fraction for each stack balance:
Simple starter — discover the successor function (add one):
Not bad: duplicate the number twice, divide by itself to get the constant 1, and add that to the original number. However, it fails to work if we add the successor of 0 as another test case:
Nothing coming — because zero division made the last test fail. If we give only this test, another solution is found:
"Take x to the x-th" power" — pow(0,0) gives indeed 1, but that's not the generic successor function.
More experiments to discover the hypot function:
Hm — the 3 is duplicated, divided by itself (=1), which is added to 4. Try to swap the inputs:
Another dirty trick: get square root of 4, add to 3 — presto, 5. The correct hypot function would be
but my program set (nmax=30000) ends at 5-byte codes, so even by giving another test to force discovery of the real thing, it would never reach a 7-byte code. OK, I bite the bullet, set nmax to 500000, wait 5 minutes for the partitioning, and then:
Hm.. cheap trick again — it was discovered that the solution is just the successor of the second argument. Like in real life, test cases have to be carefully chosen. So I tried with another a^2+b^2=c^2 set, and HEUREKA! (after 286 seconds):
After partitioning, 54005 programs had the -1 stack balance, and the correct result was on position 48393 in that list...
And finally, with the half-million set of programs, here's a solution for the successor function too:
"d-" subtracts top of stack from itself, pushing 0; the second duplicate to the 0-th power gives 1, which is added to the original argument. After some head-scratching, I find it plausible, and possibly it is even the simplest possible solution, given the poorness of this RPN language.
Lessons learned:
* Brute force is simple, but may demand very much patience (or faster hardware)
* The sky, not the skull is the limit what all we can do with Tcl :)

### Object orientation

OO (Object Orientation) is a style in programming languages popular since Smalltalk, and especially C++, Java, etc. For Tcl, there have been several OO extensions/frameworks (incr Tcl, XOTcl, stooop, Snit to name a few) in different flavors, but none can be considered as standard followed by a majority of users. However, most of these share the features
* classes can be defined, with variables and methods
* objects are created as instances of a class
* objects are called with messages to perform a method
Of course, there are some who say: "Advocating object-orientated programming is like advocating pants-oriented clothing: it covers your behind, but often doesn't fit best" ...

#### Bare-bones OO

Quite a bunch of what is called OO can be done in pure Tcl without a "framework", only that the code might look clumsy and distracting. Just choose how to implement instance variables:
* in global variables or namespaces
* or just as parts of a transparent value, with TOOT
The task of frameworks, be they written in Tcl or C, is just to hide away gorey details of the implementation — in other words, sugar it :) On the other hand, one understands a clockwork best when it's outside the clock, and all parts are visible — so to get a good understanding of OO, it might be most instructive to look at a simple implementation.
As an example, here's a Stack class with push and pop methods, and an instance variable s — a list that holds the stack's contents:
The interp alias makes sure that calling the object's name, like
is understood and rerouted as a call to the dispatcher below:
The dispatcher imports the object's variables (only s here) into local scope, and then switches on the method name:
A framework would just have to make sure that the above code is functionally equivalent to, e.g. (in a fantasy OO style):
which, I admit, reads definitely better. But bare-bones has its advantages too: in order to see how a clockwork works, you'd better have all parts visible :)
Now testing in an interactive tclsh:

#### TOOT: transparent OO for Tcl

Transparent OO for Tcl, or TOOT for short, is a very amazing combination of Tcl's concept of transparent values, and the power of OO concepts. In TOOT, the values of objects are represented as a list of length 3: the class name (so much for "runtime type information" :-), a "|" as separator and indicator, and the values of the object, e.g.
Here's my little take on toot in a nutshell. Classes in C++ started out as structs, so I take a minimal struct as example, with generic get and set methods. We will export the get and set methods:
The two generic accessor functions will be inherited by "struct"s
The set method does not change the instance (it couldn't, as it sees it only "by value") — it just returns the new composite toot object, for the caller to do with it what he wants:
For the whole thing to work, here's a simple overloading of unknown — see "Let unknown know". It augments the current unknown code, at the top, with a handler for
patterns, which converts it to the form
and returns the result of calling that form:
Now to use it (I admit the code is no easy reading):
Testing: we define a "struct" named foo, with two obvious members:
Create an instance as pure string value:
Modify part of the foo, and assign it to another variale:
Struct-specific methods can be just procs in the right namespace. The first and second arguments are the class (disregarded here, as the dash shows) and the value, the rest is up to the coder. This silly example demonstrates member access and some string manipulation:

### A little deterministic Turing machine

At university, I never learned much about Turing machines. Only decades later, a hint in the Tcl chatroom pointed me to http://csc.smsu.edu/~shade/333/project.txt , an assignment to implement a Deterministic Turing Machine (i.e. one with at most one rule per state and input character), which gives clear instructions and two test cases for input and output, so I decided to try my hand in Tcl.
Rules in this little challenge are of the form a bcD e, where
* a is the state in which they can be applied
* b is the character that must be read from tape if this rule is to apply
* c is the character to write to the tape
* D is the direction to move the tape after writing (R(ight) or L(eft))
* e is the state to transition to after the rule was applied
Here's my naive implementation, which takes the tape just as the string it initially is. I only had to take care that when moving beyond its ends, I had to attach a space (written as _) on that end, and adjust the position pointer when at the beginning. Rules are also taken as strings, whose parts can easily be extracted with string index — as it's used so often here, I alias it to @. }
Test data from http://csc.smsu.edu/~shade/333/project.txt
Testing:
reports the results as wanted in the paper, on stdout:

### Streams

Streams are a powerful concept in (not only functional) programming. In SICP chapter 3.5, streams are introduced as data structures characterized as "delayed lists", whose elements are produced and returned only on demand (deferred evaluation). This way, a stream can promise to be a potentially endless source of data, while taking only finite time to process and deliver what's really wanted. Other streams may provide a finite but very large number of elements, which would be impractical to process in one go. In Tcl, the two ways of reading a file are a good example:
* read $fp returns the whole contents, which then can be processed;
* while {[gets $fp line]>-1} {...} reads line by line, interleaved with processing
The second construct may be less efficient, but is robust for gigabyte-sized files. A simpler example is pipes in Unix/DOS (use TYPE for cat there):
where the "cat" delivers lines of the file as long as "more" will take them, and waits otherwise (after all, stdin and stdout are just streams...). Such process chains can be emulated in Tcl with the following rules:
A stream is modelled here as a procedure that returns one stream item on each call. The special item "" (the empty string) indicates that the stream is exhausted. Streams are interesting if they don't deliver the same result on every call, which requires them to maintain state between calls e.g. in static variables (here implemented with the fancy remember proc) — examples are intgen that delivers ever increasing integers, or gets $fp where the file pointer advances at each call, so potentially all lines of the file are returned over time.
A filter takes one or more streams, and possibly other arguments, and reacts like a stream too. Hence, streams can be (and typically are) nested for processing purposes. If a filter meets end-of-stream, it should return that too. Filters may be characterized as "selectors" (who may return only part of their input, like "grep") and/or "appliers" who call a command on their input and return the result. Note that on infinite streams, selectors may never return, e.g. if you want the second even prime... Streams in general should not be written in brackets (then the Tcl parser would eagerly evaluate them before evaluating the command), but braced, and stream consumers eval the stream at their discretion.
Before we start, a word of warning: maintaining state of a procedure is done with default arguments that may be rewritten. To prevent bugs from procedures whose defaults have changed, I've come up with the following simple architecture — procs with static variables are registered as "sproc"s, which remembers the initial defaults, and with a reset command you can restore the initial values for one or all sprocs:
Now let's start with a simple stream source, "cat", which as a wrapper for gets returns the lines of a file one by one until exhausted (EOF), in which case an empty string is returned (this requires that empty lines in the files, which would look similarly, are represented as a single blank):
Usage example:
which crudely emulates the Unix/DOS pipe mentioned above (you'll have to hit ↵ Enter after every line, and q↵ Enter to quit..). more is the most important "end-user" of streams, especially if they are infinite. Note however that you need stdin for this implementation, which excludes wishes on Windows (one might easily write a UI-more that reacts on mouse clicks, though).
A more generic filter takes a condition and a stream, and on each call returns an element of the input stream where the condition holds — if ever one comes along:
Friends of syntactic sugar might prefer shell style:
and guess what, we can have that in Tcl too (and not in Scheme !-), by writing a proc, that also resets all sprocs, with the fancy name "$" (in Unix, this could be the shell prompt that you don't type, but for Tcl we always have to have the command name as first word):
To prove that we haven't cheated by using exec, let's introduce a line counter filter:
This can be added to filter chains, to count lines in the original file, or only the results from grep:
We further observe that more has a similar structure to filter, so we could also rewrite it in terms of that:
The sort filter is unusual in that it consumes its whole (finite!) input, sorts it, and acts as a stream source on the output:
Now for the example in SICP: find the second prime in the interval between 10000 and 1000000.
Another idea from SICP is a "smoothing" function, that averages each pair of values from the input stream. For this we need to introduce a short-term memory also in the filter:
which, tested on a n-element stream, returns n-1 averages:
Yet another challenge was to produce an infinite stream of pairs {i j} of positive integers, i <= j, ordered by their sum, so that more pairs produces consecutively
Here's my solution which does that:
Ramanujan numbers: The pairs generator can be used to find Ramanujan numbers, which can be represented as the sum of two integer cubes in more than one way. Here I use a global array for recording results:
delivers in hardly noticeable time the R. numbers 1729, 4104, 13832... Or, how's this infinite Fibonacchi number generator, which on more fibo produces all the F.numbers (0,1,1,2,3,5,8,13,21...) you might want?
Discussion: With the above code, it was possible to reproduce quite some behavior of streams as documented in SICP, not as data structures but with Tcl procs (though procs are data too, in some sense...). What's missing is the capability to randomly address parts of a stream, as is possible in Scheme (and of course their claim to do without assignment, or mutable data...) Tcl lists just don't follow LISP's CAR/CDR model (though KBK demonstrated in Tcl and LISP that this structure can be emulated, also with procs), but rather C's flat *TclObject style. The absence of lexical scoping also led to constructs like sproc/reset, which stop a gap but aren't exactly elegant — but Tcl's clear line between either local or global variables allows something like closures only by rewriting default arguments like done in remember (or like in Python).
Don't take this as a fundamental critique of Tcl, though — its underlying model is far more simple and elegant than LISP's (what with "special forms", "reader macros"...), and yet powerful enough to do just about everything possible...

### Playing with Laws of Form

After many years, I re-read
which is sort of a mathematical thriller, if you will. Bertrand Russell commented that the author "has revealed a new calculus, of great power and simplicity" (somehow sounds like Tcl ;^). In a very radical simplification, a whole world is built up by two operators, juxtaposition without visible symbol (which could be likened to or) and a overbar-hook (with the meaning of not) that I can't type here — it's a horizontal stroke over zero or more operands, continued at right by a vertical stroke going down to the baseline. In these Tcl experiments, I use "" for "" and angle-brackets <> for the overbar-hook (with zero or more operands in between).
One point that was new for me is that the distinction between operators and operands is not cast in stone. Especially constants (like "true" and "false" in Boolean algebras) can be equally well expressed as neutral elements of operators, if these are considered variadic, and having zero arguments. This makes sense, even in Tcl, where one might implement them as
which, when called with no arguments, return 1 or 0, respectively. So [or] == 0 and [and] == 1. In Spencer-Brown's terms, (which is "", the empty string with no arguments) is false ("nil" in LISP), and [<>] is the negation of "", i.e. true. His two axioms are:
and these can be implemented by a string map that is repeated as long as it makes any difference (sort of a trampoline) to simplify any expression consisting only of operators and constants (which are operators with zero arguments):
Testing:
which maps <><> to <>, <<>> to "", and returns <> for "true".
In the algebra introduced here, with a variable "a", no further simplification was so far possible. Let's change that — "a" can have only two values, "" or <>, so we might try to solve the expression by assuming all possible values for a, and see if they differ. If they don't, we have found a fact that isn't dependent on the variable's value, and the resulting constant is returned, otherwise the unsolved expression:
with a helper function in that reports containment of an element in a list:
Testing:
which means, in expr terms, {(!$a || $a) == 1}, for all values of a. In other words, a tautology. All of Boole's algebra can be expressed in this calculus:
We can test it with the classic "ex contradictione quodlibet" (ECQ) example — "if p and not p, then q" for any q:
So formally, q is true, whatever it is :) If this sounds overly theoretic, here's a tricky practical example in puzzle solving, Lewis Carroll's last sorites (pp. 123f.). The task is to conclude something from the following premises:
* The only animals in this house are cats
* Every animal is suitable for a pet, that loves to gaze at the moon
* When I detest an animal, I avoid it
* No animals are carnivorous, unless they prowl at night
* No cat fail to kill mice
* No animals ever take to me, except what are in this house
* Kangaroos are not suitable for pets
* None but carnivora kill mice
* I detest animals that do not take to me
* Animals that prowl at night always love to gaze at the moon
These are encoded to the following one-letter predicates:

#### a

    avoided by me

#### c

    cat

#### d

    detested by me

#### h

    house, in this

#### k

    kill mice

#### m

    moon, love to gaze at

#### n

    night, prowl at

#### p

    pet, suitable for

#### r

    (kanga)roo

#### t

    take to me

#### v

    (carni)vorous
So the problem set can be restated, in Spencer-Brown's terms, as
I first don't understand why all premises can be just written in a row, which amounts to implicit "or", but it seems to work out well. As we've seen that <x>x is true for any x, we can cancel out such tautologies. For this, we reformat the expression to a list of values of type x or !x, that is in turn dumped into a local array for existence checking. And when both x and !x exist, they are removed from the expression:
which results in:
* a <r>
translated back: "I avoid it, or it's not a kangaroo", or, reordered, "<r> a" which by (4) means, "All kangaroos are avoided by me".

### A little IRC chat bot

Here is a simple example of a "chat bot" — a program that listens on an IRC chatroom, and sometimes also says something, according to its programming. The following script
* connects to channel #tcl on IRC
* listens to what is said
* if someone mentions its name (minibot), tries to parse the message and answer.
Examples from the chat:

## Tk: the cross-platform GUI toolkit
### Introduction

The Tk (Tool Kit) is the most popular Tcl extension for designing graphical user interfaces (GUI) on Macintosh, Unix/Linux, or Windows operating systems.
With little effort, it allows to put together useful
* windows (forms, as some call them) consisting of
* widgets, which are managed by
* geometry managers. Also, you can easily define
* bindings of mouse or keyboard events to trigger the actions you want.

#### Example: calculator

Here is a very simple, complete Tcl/Tk script that implements a calculator:
It creates an entry widget named .e, into which you can type from the keyboard, with an associated variable e (which will mirror the entry's content), and manages it with pack.
One binding is defined: if with keyboard focus on .e, the <Return> key is hit, then
* all division operators (/) in the variable e are mapped to "*1./" (this forces floating point division),
* the resulting string is fed to expr to evaluate it as an arithmetic/logic expression
* as there may be errors in the user input, the expr call is wrapped into a catch which assigns either the result of expr, or the error message if one occurred, into the variable res
* the result of the last evaluation is cleared by deleting everything after =
* finally, an equal sign and the value of the res variable are appended to e, making input and result immediately visible in the entry.

#### Example: a tiny IRC client

As before in the Tcl section, here's a working little script again: a client for IRC (Internet Relay Chat) in 38 lines of code, that features a text and an entry widget:
The last line is a quick debugging helper: if you modified the script in the editor, save to disk, then hit <Escape> in the app to start it anew.

### Widgets

Widgets are GUI objects that, when mapped by a geometry manager, correspond to a rectangular area on the screen, with different properties and capabilities.
Widgets are named with path names, somewhat resembling file system path names, except that the separator for widgets is a period ".". For example, .foo.bar is a child of foo which is a child of "." Parent-child relation occurs typically with containers, e.g. toplevel, frame, canvas, text. The name parts (i.e. between the dots) can be almost any string that does not contain "." of course, or starts with a Capital Letter (this is because widget class names with capital initial have a special meaning, they denote class-wide options).
If you have a proc that takes a parent widget w and adds children, it may be important to distinguish whether w is "." or another - because you can't concatenate a child widget name directly to "." - $w.kid will make an invalid name if $w == ".". Here's how to avoid this:
Widgets are created by a command named after their class, which then takes the path name (requiring that parent widgets have to exist by that time) and any number of -key value options that make up the original configuration, e.g.
After creation, communication with the widget goes via a created command that corresponds to its name. For instance, the configuration options can be queried
or changed
Some "methods" like cget, configure are generic to all widget classes, others are specific to one or a few. For instance, both text and entry accept the insert method. See the manual pages for all the details.
Widgets appear on screen only after they have been given to a geometry manager (see more below). Example:
As widget creation commands return the pathname, these two steps can also be nested like
The destroy command deletes a widget and all of its child widgets, if present:
The following sub-chapters describe the widgets available in Tk.

#### button

With text and/or image, calls a configurable command when clicked. Example:

#### canvas

Scrollable graphic surface for line, rectangle, polygon, oval, and text items, as well as bitmaps and photo images and whole embedded windows. See for example "A tiny drawing program" below. Example:
Pan a canvas (scroll it inside its window with middle mouse-button held down) by inheriting the text widget bindings:

#### entry

One-line editable text field, horizontally scrollable (see example above). You can specify a validation function to constrain what is entered. Example:

#### frame

Container for several widgets, often used with pack, or for wrapping "megawidgets"

#### label

One- or multiline field for text display, can be tied to a text variable to update when that variable changes. Linebreaks are specified by \n in the text to be displayed.

#### labelframe

A container similar to a frame, decorated with a thin rectangle around, and a label at top-left position. Example (a tiny radio band selector):

#### listbox

Multiline display of a list, scrollable. Single or multiple items can be selected with the mouse.

#### menu

To add a menu to a GUI application, take steps of three kinds:
* Create the toplevel horizontal menu (needed only once):
* For each item in the top menu, create a cascaded submenu, e.g.
* For each entry in a submenu, add it like this:
As these commands are a bit verbose, one can wrap them into a little helper:
Demo example - now menu items can be created in a much clearer way:

#### radiobutton

A button with a selector field which can be on or off, and a label. Clicking the selector field with the mouse changes the value of an associated global variable. Typically, multiple radiobuttons are tied to the same variable. For examples, see labelframe above.

#### scrollbar

Horizontal or vertical scrollbars (vertical being the default, for horizontal specify: -orientation horizontal, or -ori hori if you like it short) can be tied to canvas, entry, listbox or text widgets. The interaction between a scrollbar and its scrolled widget goes with callbacks, in which one notifies the other:
* scrollbar to widget: xview or yview method
* widget to scrollbar: set method
Arguments to these methods will be automatically added when the callbacks are invoked.
For example, here's how to connect a text widget with a vertical scrollbar:
With most widgets, scrollbars adjust automatically to the widget's contents. For canvas widgets, you need to update the scrollregion after adding new items. most simply like this:

#### text

Scrollable editable multiline text with many formatting options. Can also contain images and embedded widgets. The default wrapping setting is "none", so you might need a horizontal scrollbar to see all of long lines. In many cases it's more user-friendly to configure a text widget as -wrap word - then you only need a vertical scrollbar at most.
Positions in a text widget are specified as line.column, where line starts from 1, and column from 0, so 1.0 is the very first character in a text widget. Example how to delete all contents:
For highlighting part of the contents, you can define tags and assign them to subsequences:

#### toplevel

Standalone frame window, mostly with decorations (title bar, buttons) from the window manager. When you start Tk, you receive an initially empty toplevel named "." (one dot). If you want more toplevels, create them like this:
Such toplevels are logically children of ".". To assign a nice title to a toplevel, use
You can also control the geometry (size and position) of a toplevel with

### Geometry managers

The purpose of geometry managers is to compute the required height, width, and location of widgets, and map them on screen. Besides grid, pack and place, the canvas and text widgets can also manage embedded widgets.
Calls to geometry managers always start with the manager's name, then (mostly) one or more widget names, then any number of -name value options

#### grid

This geometry manager is best suited for tabular window layout consisting of rows and columns. Example, to put three widgets in a horizontal row:
The -sticky option indicates what side of its box the widget should stick to, in compass direction; "news" is north-east-west-south", i.e. all four sides.
Here's a minimal solution to display a table (list of lists) in a grid of labels:

#### pack

This used to be the workhorse manager, but in recent years has been less popular than grid. Anyway, it is still good for cases where you have widgets aligned in only one direction (horizontally or vertically). For more complex layouts, one used to insert intermediate frames, but grid makes such jobs just easier. Example:

#### place

This geometry manager is not often used, mostly for special applications, like when you want to highlight the current tab of a tab notebook. It allows pixel-precise placement of widgets, but is less dynamic in reaction to resizing of the toplevel or inner widgets.

### Dialogs

Dialogs are toplevels that are to tell a message, or answer a question. You don't have to assign a widget path name to them. Just call them as functions and evaluate the result (often "" if the dialog was canceled).

#### tk_getOpenFile

A file selector dialog (limited to existing files). Returns the selected file with path name, or "" if canceled.

#### tk_getSaveFile

A file selector dialog, which also allows specification of a not existing file. Returns the selected file with path name, or "" if canceled.

#### tk_messageBox

A simple dialog that displays a string and can be closed with an "OK" button. Example:

#### tk_chooseColor

Displays a dialog for color selection. The returned value is the selected color in one of the representations conformant to Tcl's comprehension of such information; on Microsoft Windows systems this might constitute a hexadecimal string in the format “#RRGGBB”. Upon abortion of the process, the empty string is instead delivered. The dialog may be configured to preselect a certain default color via the “-initialcolor” option, a subordination into a parent widget with “-parent”, and a title caption through “-title”.

#### Custom dialogs

Besides the prefabricated dialogs that come with Tk, it's also not too hard to build custom ones. As a very simple example, here's a "value dialog" that prompts the user for to type in a value:
Test:
For a more elaborate example, here is a record editor dialog (multiple fields, each with a label and entry (or text for multi-line input)):
Quick test:

### Megawidgets made easy

The term "megawidgets" is popular for compound widgets that in themselves contain other widgets, even though they will hardly number a million (what the prefix Mega- suggests), more often the child widgets' number will not exceed ten.
To create a megawidget, one needs one proc with the same signature as Tk widget creation commands. This proc will, when called, create another proc named after the widget, and dispatch methods either to specific handlers, or the generic widget command created by Tk.

#### A little notebook

Plain Tk does not contain a "notebook" widget, with labeled tabs on top that raise one of the "pages", but it's easy to make one. This example demonstrates how the tabs are implemented as buttons in a frame, and how the original Tk command named like the frame is "overloaded" to accept the additional add and raise methods:
Test and demo code:

### Binding events

Events within Tcl/Tk include actions performed by the user, such as pressing a key or clicking the mouse. To react to mouse and keyboard activity, the bind command is used. As shown in the calculator example:
The bind keyword operates on .e and associates the event related to the <return> event. The following bracket indicates a start of a set of procedures which are executed when the event is performed.

### BWidget

BWidget is an extension to Tk written in pure Tcl (therefore it can even run on Windows Mobile-driven cell phones). It offers mega-widgets (all class names starting with Uppercase) like
* ComboBox
* NoteBook
* Tree
Screenshot of a NoteBook and a Tree, on a PocketPC under Windows/CE

#### Tree examples

Here is a "hello world" example of a Tree widget (this is a complete script). The root node is constantly called root, for the others you have to make up names:
The famous typewriter test sentence represented as a syntax tree:

### Tk resources
#### Colors

Colors in Tk can be specified in three ways:
* a symbolic name, like: red green blue yellow magenta cyan
* a hex string preceded by a #: #RGB, #RRGGBB, #RRRRGGGGBBBB
* a list of three non-negative integers
The last form is only returned by commands. To specify a color to a command, you'll have to hex-format it. For instance, white could be described as #FFFFFF.
To turn a symbolic name into its RGB components:
Here is the list of defined color names (as from X11's rgb.txt):
In addition, the following are defined on Windows:

#### Cursors

For every widget, you can specify how the mouse cursor should look when over it. Here's the list of defined cursor names:
A little tool that presents the cursor names, and shows each cursor shape when mousing over:

#### Fonts

Fonts are provided by the windowing system. Which are available depends on the local installation. Find out which fonts are available with
The typical description of a font is a list of up to three elements:
Example:
Family is a name like Courier, Helvetica, Times, ... Best pick one of the names delivered by font families, though there may be some mappings like "Helvetica" -> "Arial"
Size is point size (a typographer's point is 1/72th of an inch) if positive, or pixel size if negative. Normal display fonts often have a size of 9 or 10.
Style can be a list of zero or more of bold, italic, underlined, ...

#### Images: photos and bitmaps

Tk allows simple yet powerful operations on images. These come in two varieties: bitmaps and photos. Bitmaps are rather limited in functionality, they can be specified in XBM format, and rendered in configurable colors.
Photos have much more possibilities - you can load and save them in different file formats (Tk itself supports PPM and GIF - for others, use the Img extension), copy them with many options, like scaling/subsampling or mirroring, and get or set the color of single pixels.
Setting can also do rectangular regions in one go, as the following example shall demonstrate that creates a photo image of a tricolore flag (three even-spaced vertical bands - like France, Italy, Belgium, Ireland and many more). The default is France. You can specify the width, the height will be 2/3 of that. The procedure returns the image name - just save it in -format GIF if you want:

### Debugging Tk programs

A Tk program under development can be very rapidly debugged by adding such bindings:
This works only on Windows and Macintosh (pre-OS-X) and brings up a console in which you can interact with the Tcl interpreter, inspect or modify global variables, configure widgets, etc.
This starts a new instance of the current program (assuming you edited a source file and saved it to disk), and then terminates the current instance.
For short debugging output, one can also use the window's title bar. For example, to display the current mouse coordinates when it moves:

### Other languages

Other programming languages have modules that interface and use Tcl/Tk:
* In R (programming language), there's a tcltk library, invoked with the command library(tcltk)
* In Python, there's a tkinter module, invoked with import tkinter or from tkinter import *
* Common Lisp can communicate with Tcl/Tk via several externally available libraries, including CL-TK and LTK

## Tk examples

The following examples originally appeared in the Tcler's Wiki http://wiki.tcl.tk . They are all in the public domain - no rights reserved.

### A funny cookbook

This funny little program produces random cooking recipes. Small as it is, it can produce 900 different recipes, though they might not be to everybody's taste... The basic idea is to pick an arbitrary element from a list, which is easily done in Tcl with the following:
This is used several times in:
And as modern programs always need a GUI, here is a minimal one that appears when you source this file at top level, and shows a new recipe every time you click on it:
Enjoy!

### A little A/D clock

This is a clock that shows time either analog or digital - just click on it to toggle.

### A little pie chart

Arc elements of a canvas are by default rendered as pie slices (part of the circumference of a circle, connected by radius lines to the center. Hence it s rather easy to produce a pie chart. The following code is a bit more complex, as it also determines positions for the labels of the pies:
Testing:

### A little 3D bar chart

The following script displays a bar chart on a canvas, with pseudo-3-dimensional bars - a rectangle in front as specified, embellished with two polygons - one for the top, one for the side:}
For a more plastic look, the fill color of the polygons is reduced in brightness ("dimmed"):
Draw a simple scale for the y axis, and return the scaling factor:
An interesting sub-challenge was to round numbers very roughly, to 1 or maximally 2 significant digits - by default rounding up, add "-" to round down:
So here is my little bar chart generator. Given a canvas pathname, a bounding rectangle, and the data to display (a list of {name value color} triples), it figures out the geometry. A gray "ground plane" is drawn first. Note how negative values are tagged with "d"(eficit), so they look like they "drop through the plane".
Generally useful helper functions:
Testing the whole thing (see screenshot):

### A little calculator

Here is a small calculator in Tcl/Tk. In addition to the buttons on screen, you can use any of expr's other functionalities via keyboard input.
And, as Cameron Laird noted, this thingy is even programmable: enter for example
into the entry, disregard warnings; now you can do
and receive [fac 10] = 3628800.0 as result...

### A little slide rule

The slide rule was an analog, mechanical device for approximate engineering computing, made obsolete by the pocket calculator since about the 1970-80s. The basic principle is that multiplication is done by adding logarithms, hence most of the scales are logarithmic, with uneven increments.
This fun project recreates a slide rule (roughly an Aristo-Rietz Nr. 89 with 7 scales - high-notch ones had up to 24) with a white "body" and a beige "slide" which you can move left or right with mouse button 1 clicked, or in pixel increment with the <Shift-Left>/<Shift-Right> cursor keys. Finally, the blue line represents the "mark" (how is that correctly called? "runner"? "slider"?) which you can move with the mouse over the whole thing to read a value. Fine movements with <Left>/<Right>.
Due to rounding errors (integer pixels), this plaything is even less precise than a physical slide rule was, but maybe you still enjoy the memories... The screenshot shows how I found out that 3 times 7 is approx. 21... (check the A and B scales).

### A minimal doodler

Here is a tiny but complete script that allows doodling (drawing with the mouse) on a canvas widget:
And here it comes again, but this time with explanations:
The "Application Program Interface" (API) for this, if you want such ceremonial language, is the doodle command, where you specify which canvas widget should be enabled to doodle, and in which color (defaults to black):}
It registers two bindings for the canvas, one (<1>) when the left mouse-button is clicked, and the other when the mouse is moved with button 1 (left) down. Both bindings just call one internal function each.
On left-click, a line item is created on the canvas in the specified fill color, but with no extent yet, as start and end points coincide. The item ID (a number assigned by the canvas) is kept in a global variable, as it will have to persist long after this procedure has returned:
The left-motion procedure obtains the coordinates (alternating x and y) of the globally known doodling line object, appends the current coordinates to it, and makes this the new coordinates - in other words, extends the line to the current mouse position:
That's all we need to implement doodling - now let's create a canvas to test it, and pack it so it can be drawn as big as you wish:
And this line turns on the doodle functionality created above (defaulting to black):
Add a binding for double-right-click/double-button-3, to clear the canvas (added by MG, Apr 29 04)

### A tiny drawing program

Here is a tiny drawing program on a canvas. Radio buttons on top allow choice of draw mode and fill color. In "Move" mode, you can of course move items around. Right-click on an item to delete it.
A radio is an obvious "megawidget" to hold a row of radiobuttons. This simple one allows text or color mode: }
Depending on draw mode, the mouse events "Down" and "Motion" have different handlers, which are dispatched by names that look like array elements. So for a mode X, we need a pair of procs, down(X) and move(X). Values used between calls are kept in global variables.
First, the handlers for free-hand line drawing:
Polygons are drawn by clicking the corners. When a corner is close enough to the first one, the polygon is closed and drawn.
For saving the current image, you need the Img extension, so just omit the following binding if you don't have Img:

### A minimal editor

Here's an utterly simple editor, in 26 lines of code, which just allows to load and save files, and of course edit, and cut and paste, and whatever is built-in into the text widget anyway. And it has a bit "online help"... ;-)
It is always a good idea to start a source file with some explanations on the name, purpose, author, and date. I have recently picked up the habit to put this information into a string variable (which in Tcl can easily span multiple lines), so the same info is presented to the reader of the source code, and can be displayed as online help: }
The visible part of a Graphical User Interface (GUI) consists of widgets. For this editor, I of course need a text widget, and a vertical scrollbar. With the option "-wrap word" for the text widget, another horizontal scrollbar is not needed - lines longer than the window just wrap at word boundaries.
Tk widgets come on the screen in two steps: first, they are created with an initial configuration; then, handed to a "geometry manager" for display. As widget creation commands return the pathname, they can be nested into the manager command (pack in this case), to keep all settings for a widget in one place. This may lead to over-long lines, though.
Although the scrollbar comes to the right of the text, I create and pack it first. The reason is that when a window is made smaller by the user, the widgets last packed first lose visibility.
These two lines also illustrate the coupling between a scrollbar and the widget it controls:
* the scrollbar sends a yview message to it when moved
* the widget sends a set message to the scrollbar when the view changed, for instance from cursor keys
And these two lines already give us an editor for arbitrarily long text, with built-in capabilities of cut, copy, and paste - see the text man page. Only file I/O has to be added by us to make it really usable.
Are you targetting 8.4 or later? If so, add -undo 1 to the options to text and get full undo/redo support!
The other important part of a GUI are the bindings - what event shall trigger what action. For simplicity, I've limited the bindings here to a few of the function keys on top of typical keyboards:
Online help is done with a no-frills tk_messageBox with the "about" text defined at start of file. - The other bindings call custom commands, which get a filename argument from Tk's file selector dialogs:
These dialogs can also be configured in a number of ways, but even in this simple form they are quite powerful - allow navigation around the file system, etc. On Windows they call the native file selectors, which have a history of previously opened files, detail view (size/date etc.)
When this editor is called with a filename on the command line, that file is loaded on startup (simple as it is, it can only handle one file at a time):
The procedures for loading and saving text both start with a sanity check of the filename argument - if it's an empty string, as produced by file selector dialogs when the user cancels, they return immediately. Otherwise, they transfer file content to text widget or vice-versa. loadText adds the "luxury" that the name of the current file is also put into the window title. Then it opens the file, clears the text widget, reads all file contents in one go, and puts them into the text widget.
saveText takes care not to save the extra newline that text widgets append at end, by limiting the range to "end - 1 c"(haracter).

### File watch

Some editors (e.g. PFE, MS Visual Studio) pop up an alert dialog when a file was changed on disk while being edited - that might lead to edit conflicts. Emacs shows a more subtle warning at the first attempt to change a file that has changed on disk.
Here I try to emulate this feature. It is oversimplified because it does not update the mtime (file modification time) to check, once you saved it from the editor itself. So make sure to call text'watch'file again after saving.
Using the global variable ::_twf it is at least possible to avoid false alarms - for a more serious implementation one might use a namespaced array of watchers, indexed by file name, in case you want multiple edit windows. }
The dialog should come up when you change the file externally, say by touch-ing it in pure Tcl, which might be done with editing it in another editor, or

### Tiny presentation graphics

This is a crude little canvas presentation graphics that runs on PocketPCs, but also on bigger boxes (one might scale fonts and dimensions there). Switch pages with Left/Right cursor, or left/right mouseclick (though a stylus cannot right-click).
Not many features, but the code is very compact, and with a cute little language for content specification, see example at end (which shows what I presented at the 2003 Euro-Tcl convention in Nuremberg...)}
The rest is data - or is it code? Anyway, here's my show:

### Timeline display

Yet another thing to do with a canvas: history visualisation of a horizontal time-line, for which a year scale is displayed on top. The following kinds of objects are so far available:
* "eras", displayed in yellow below the timeline in boxes
* "background items" that are grey and stretch over all the canvas in height
* normal items, which get displayed as stacked orange bars
You can zoom in with <1>, out with <3> (both only in x direction). On mouse motion, the current year is displayed in the toplevel's title. Normal items can be a single year, like the Columbus example, or a range of years, for instance for lifetimes of persons. (The example shows that Mozart didn't live long...)
This command adds an object to the canvas. The code for "item" took me some effort, as it had to locate a free "slot" on the canvas, searching top-down:
Here's a sample application, featuring a concise history of music in terms of composers:
These nifty shorthands for adding items make data specification a breeze - compare the original call, and the shorthand:
With an additional "!" argument you can make the text of an item bold:
Now for the data to display (written pretty readably):

### Fun with functions

My teenage daughter hates math. In order to motivate her, I beefed up an earlier little function plotter which before only took one function, in strict Tcl (expr) notation, from the command line. Now there's an entry widget, and the accepted language has also been enriched: beyond exprs rules, you can omit dollar and multiplication signs, like 2x+1, powers can be written as x3 instead of ($x*$x*$x); in simple cases you can omit parens round function arguments, like sin x2. Hitting <Return> in the entry widget displays the function's graph.
If you need some ideas, click on the "?" button to cycle through a set of demo functions, from boring to bizarre (e.g. if rand is used). Besides default scaling, you can zoom in or out. Moving the mouse pointer over the canvas displays x and y coordinates, and the display changes to white if you're on a point on the curve.
The target was not reached: my daughter still hates math. But at least I had hours of Tcl (and function) fun again, surfing in the Cartesian plane... hope you enjoy it too!

### Functional imaging

In Conal Elliott's Pan project ("Functional Image Synthesis", [1]), images (of arbitrary size and resolution) are produced and manipulated in an elegant functional way. Functions written in Haskell (see Playing Haskell) are applied, mostly in functional composition, to pixels to return their color value. FAQ: "Can we have that in Tcl too?"
As the funimj demo below shows, in principle yes; but it takes some patience (or a very fast CPU) - for a 200x200 image the function is called 40000 times, which takes 9..48 seconds on my P200 box. Still, the output often is worth waiting for... and the time used to write this code was negligible, as the Haskell original could with few modifications be represented in Tcl. Functional composition had to be rewritten to Tcl's Polish notation - Haskell's
(where "o" is the composition operator) would in Tcl look like
As the example shows, additional arguments can be specified; only the last argument is passed through the generated "function nest":
But the name of the generated function is much nicer than "f": namely, the complete call to "o" is used, so the example proc has the name
which is pretty self-documenting ;-) I implemented "o" like this:
Basic imaging functions ("drawers") have the common functionality point -> color, where point is a pair {x y} (or, after applying a polar transform, {r a}...) and color is a Tk color name, like "green" or #010203:
Beyond the examples in Conal Elliott's paper, I found out that function imaging can also be abused for a (slow and imprecise) function plotter, which displays the graph for y = f(x) if you call it with $y + f($x) as first argument:
Here is a combinator for two binary images that shows in different colors for which point both or either are "true" - nice but slow:}
This painter colors a grayscale image in hues of the given color. It normalizes the given color through dividing by the corresponding values for "white", but appears pretty slow too:
This proc caches the results of [winfo rgb] calls, because these are quite expensive, especially on remote X displays - rmax
Now comes the demo program. It shows the predefined basic image operators, and some combinations, on a button bar. Click on one, have some patience, and the corresponding image will be displayed on the canvas to the right. You can also experiment with image operators in the entry widget at bottom - hit <Return> to try. The text of sample buttons is also copied to the entry widget, so you can play with the parameters, or rewrite it as you wish. Note that a well-formed funimj composition consists of:
* the composition operator "o"
* zero or more "painters" (color -> color)
* one "drawer" (point -> color)
* zero or more "transformers" (point -> point)
Composed functions need only be mentioned once, which creates them, and they can later be picked up by info procs. The o looks nicely bullet-ish here..

### TkPhotoLab

The following code can be used for experiments in image processing, including
* convolutions (see below)
* conversion from color to greylevel
* conversion from greylevel to faux color
* brightness and contrast modification
Tcl is not the fastest in heavy number-crunching, as needed when going over many thousands of pixels, but I wouldn't consider C for a fun project ;) So take your time, or get a real CPU. At least you can watch the progress, as the target image is updated after every row.
File:TkPhotoLab.jpg
Edge enhancement by Laplace5 filter
The demo UI shows two images, the original on the left, the processing result on the right. You can push the result to the left with Options/Accept. See the menus for what goodies I have supplied. But what most interested me were "convolutions", for which you can edit the matrix (fixed at 3x3 - slow enough..) and click "Apply" to run it over the input image. "C" to set the matrix to all zeroes.
Convolution is a technique where a target pixel is colored according to the sum of the product of a given matrix and its neighbors. As an example, the convolution matrix
colors the pixel in the middle with the average of itself and its eight neighbors, which will myopically blur the picture.
should just faithfully repeat the input picture. These
enhance {horizont,vertic}al edges, and make the image look "crispier". }
The following routines could also be generified into one:
An experiment in classifying graylevels into unreal colors:
A number of algorithms are very similar, distinguished only by a few commands in the center. Hence I made them generic, and they take a function name that is applied to every pixel rgb, resp. a pair of pixel rgb's. They are instantiated by an alias that sets the function fancily as a lambda:
A histogram is a count of which color value occurred how often in the current image, separately for red, green and blue. For graylevel images, the displayed "curves" should exactly overlap, so you see only the blue dots that are drawn last.
Demo UI:
A little wrapper for simplified menu creation - see below for its use:

## Appendix
### Resources

* The main page of Tcl developers is the Tcl Developer Xchange.
* Download a free binary ActiveTcl distribution at ActiveState.
* The main USENET forum dedicated to this language is news:comp.lang.tcl.
* In German, there's also http://www.self-tcl.de/forum for questions and answers
* The main user wiki is The Tclers Wiki.
* The Tclers' Chat can provide quick response to questions: Jabber all.tclers.tk:5222, IRC freenode #tcl, or Webchat http://mini.net/cgi-bin/chat.cgi (all of the three being relayed between each other)
* The Tcl man pages can be found here.
* The Tk man pages can be found here.
* Documentation on Tcllib, the Tcl standard library is here.
* There is a tutorial for Tcl here.
For Web development, there are:
* pure-Tcl Web servers like Tclhttpd, which can be embedded in your Tcl-enabled applications to provide Web-based interfaces,
* Tcl-based Web servers like AOLServer, which are much faster than their pure-Tcl cousins and able to support a heavy load, and
* Tcl Web server modules (see Apache Tcl for examples) that enable Tcl Web applications with bog-standard Web servers.

### ActiveState Community License

This is the license of the "Batteries-included" ActiveTcl distribution. Note that you cannot redistribute ActiveTcl "outside your organization" without written permission. The part between "AGREEMENT" and "Definitions:" is the more free, BSD-style license of Tcl and Tk itself.
Preamble:
The intent of this document is to state the conditions under which the Package (ActiveTcl) may be copied and distributed, such that ActiveState maintains control over the development and distribution of the Package, while allowing the users of the Package to use the Package in a variety of ways.
The Package may contain software covered by other licenses:
TCL LICENSE AGREEMENT
This software is copyrighted by the Regents of the University of California, Sun Microsystems, Inc., Scriptics Corporation, and other parties. The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.
The authors hereby grant permission to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
GOVERNMENT USE: If you are acquiring this software on behalf of the U.S. government, the Government shall have only "Restricted Rights" in the software and related documentation as defined in the Federal Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you are acquiring the software on behalf of the Department of Defense, the software shall be classified as "Commercial Computer Software" and the Government shall have only "Restricted Rights" as defined in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the authors grant the U.S. Government and others acting in its behalf permission to use and distribute the software in accordance with the terms specified in this license.
Definitions:
"ActiveState" refers to ActiveState Corp., the Copyright Holder of the Package.
"Package" refers to those files, including, but not limited to, source code, binary executables, images, and scripts, which are distributed by the Copyright Holder.
"You" is you, if you are thinking about copying or distributing this Package.
Terms:
1. You may use this Package for commercial or non-commercial purposes without charge.
2. You may make and give away verbatim copies of this Package for personal use, or for use within your organization, provided that you duplicate all of the original copyright notices and associated disclaimers. You may not distribute copies of this Package, or copies of packages derived from this Package, to others outside your organization without specific prior written permission from ActiveState (although you are encouraged to direct them to sources from which they may obtain it for themselves).
3. You may apply bug fixes, portability fixes, and other modifications derived from ActiveState. A Package modified in such a way shall still be covered by the terms of this license.
4. ActiveState's name and trademarks may not be used to endorse or promote packages derived from this Package without specific prior written permission from ActiveState.
5. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
ActiveState Community License Copyright (C) 2001-2003 ActiveState Corp. All rights reserved.