Tcl programming

Introduction

This page reviews some basic aspects of Tcl that are central in QTcl programming. In particular the difference between immediate and delayed evaluation of variables is discussed.

To learn more about Tcl, access the Tcl documentation or the Tcl tutorial section



In this section:

Strings and Lists

Strings in Tcl are simply concatenated characters. If the string contains white-space it must be delimited by quotes:

Tcl Strings

# Legal string syntax in Tcl
set x hello
set y "hello"
set z "hello world"

If the string contains variables these are resolved immediately:

Variables Resolved in a String

set x 4
puts "He ate $x apples" ; # prints 'He ate 4 apples'

Lists are elements delimited by '{...}' or created using the list command. The elements may be any variable or data structure allowed in Tcl, including other lists.

Creating Lists

set l { He ate 4 "green apples" }
set l [list He ate 4 "green apples" ]

This lists contains 4 elements, the last element being "green apples."

Evaluation of Variables

Lists created with braces differ from strings in that variables are not resolved when the list is declared.

Simple List

set x 4
puts "He ate $x apples"   ; # prints 'He ate 4 apples'
puts { He ate $x apples } ; # prints 'He ate $x apples'

The use of braces to preserve variables is useful when statements are to be evaluated at a later point of execution. This mode of programming is greatly used when creating interface menus, when so-called callback functions are assigned to various elements of the menu. The callback functions will be called in response to a window event, which may happen at any time.

Callback Functions

set x 4
qpushbutton "Immediate" "qinformation Information $x" ; # displays '4'
qpushbutton "Delayed" { qinformation Information $x } ; # displays '5'
set x 5

In current example showing Callback Functions, two buttons are created which both display the value of x when activated. The first button is given a callback string which immediately resolves to 'qinformation 4'. This string is stored together with the button and is implemented at the same time the user activates the button. By this time x has been set to '5', but that no longer affects the contents of the callback string.

The second button is given a brace-delimited list as the callback function. The variable x is not resolved and the list { qinformation $x } is assigned as callback function regardless of the value of x. When the user later activates the button however, x holds the value '5', and this is displayed on the output channel.

Command Substitution

A special form of command evaluation involves the square brackets '[ref: ... ]'. When encountering an expression in square brackets, Tcl will evaluate the expression inside, and replace the expression in brackets with the result before evaluating the outer statement.

Command Substitution

set result [expr 2 + 2 ]
puts $result

In the above example the command 'expr 2 + 2' is evaluated and replaced with it's result, which is '4'. Any valid Tcl statement that returns a value can be placed inside square brackets. The list command, for example, returns a new list formed by concatenating the elements provided as arguments :

Nested List Expression

set l [list 1 2 3]

Here the variable 'l' is assigned and the value returned by the bracketed expression, which in this case creates a list.