Previous
Introduction 
Next
Hello World 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Basic Syntax of Tcl

Basic Syntax of Tcl

Before beginning I would like to scare you a little. If you see the following code and run away scared, well, there would be one less Tcl programmer and thus, less competition for good Tcl programmers like me. If you are still brave or pretending to be brave, good for you.

In the beginning I warned you that although Tcl/Tk is an easy language to master, it is not so easy to learn. This is why. The code for adding 3 and 4 and putting the result into the variable sum is almost the same in many popular languages like C,C++,Perl,Java etc. It is :
a=3+4;
In Tcl it is :
set a [expr 3 + 4]

Still not afraid? You will be. This "weird" syntax is because Tcl scripts are made up of commands separated by new lines or semicolons. Commands all have the same basic form illustrated by the following example:

expr 3 + 4

This command computes the sum of 4 and 3 and returns the result, 7. This value is returned and is taken by the 'set' command and given to the variable 'a'.

Variables

Tcl allows you to store values in variables and use the values later in commands. The set command is used to write and read variables. For example, the following command modifies the variable x to hold the value 32:

set x 32

This is same as the C++/Perl/Java code:

x=32;

You don't need to declare variables in Tcl: a variable is created automatically the first time it is set. Tcl variables don't have types: any variable can hold any value. To use the value of a variable in a command, use variable substitution as in the following example:

expr $x*3

When a $ appears in a command, Tcl treats the letters and digits following it as a variable name, and substitutes the value of the variable in place of the name. In this example, the actual argument received by the expr command will be 32*3 (assuming that variable x was set as in the previous example). You can use variable substitution in any word of any command, or even multiple times within a word:

set cmd expr
set x 11
$cmd $x*$x


Command substitution

You can also use the result of one command in an argument of another command. This is called command substitution:

set a 44
set b [expr $a*4]

When a '[' appears in a command, Tcl treats everything between it and the matching ']' as a nested Tcl command. Tcl evaluates the nested command and substitutes its result into the enclosing command in place of the bracketed text. In the example above the second argument of the second set command will be 176.

A Note at the end : Although set b [expr $a * 4] is correct, a more efficient way of doing the same is set b [expr {$a * 4}]. This will speed up the operation as the compiler works on the latter method better.

Quotes and braces

Double-quotes allow you to specify words that contain spaces. For example, consider the following script:

set x 24
set y 18
set z "$x + $y is [expr $x + $y]"

After these three commands are evaluated variable z will have the value 24 + 18 is 42. Everything between the quotes is passed to the set command as a single word. Note that (a) command and variable substitutions are performed on the text between the quotes, and (b) the quotes themselves are not passed to the command. If the quotes were not present, the set command would have received 6 arguments, which would have caused an error.
Curly braces provide another way of grouping information into words. They are different from quotes in that no substitutions are performed on the text between the curly braces:

set z {$x + $y is [expr $x + $y]} This command sets variable z to the value "$x + $y is [expr $x + $y]".

This may be a good feature, but it plays games with the syntax highlighting feature of the editor that I use for coding tcl. Those sadistic tcl developers!

Now try out your new found knowledge - by answering this exercise.

Previous
Introduction 
Next
Hello World 
blog comments powered by Disqus
Subscribe to Feed