
The syntax of Tcl has the simple structure of a shell-style language. A command consists of one or more words separated by spaces or tabs. The first word is the true command and the following words are the arguments. Commands are separated by newlines or semi-colons in the case of more commands on the same line.
The only datatype used in Tcl is the string. This greatly facilitates the exchange of data. In fact, it is a cornerstone of the flexibility of the language. For instance, it allows data to be used as a command, i.e. as executable Tcl code. It also makes the use of lists easy, because a list is in fact a string formatted in a special format. The drawback is that there is no rigorous type checking as in other languages, so it is easy to introduce confusion (bugs). However, for its purpose as glue component, this is not very important.
Variables are created and a value assigned to the variable by the set command. To refer to the value of that variable the $ symbol is used for variable substitution.
set a 44.5
assigns to variable a the string ``44.5'' and returns 44.5
set q 22 ; set b $q
assigns to variable b the string ``22'' and returns 22
set q
returns the string 22.
Up to this point we have been using single line scripts which can be inserted interactively using the keyboard in the Tcl shell or after the Tcl prompt.
For more complex applications where longer scripts should be used, take your plain text editor, write a text file with the script and execute the file with the Tcl source command (see Using scripts from a file on page [x]).
The syntax for a Tcl command has the following general rule:
command arg1 arg2 arg3 .....
Commands can be classified as: variables, strings, lists, arithmetic expressions, operators, file I/O, flow control. All of the Tcl-commands return a string as a result value, which can be used as an argument for following commands, a procedure known as "nesting". This command substitution is invoked by enclosing a command in brackets [ ].
For example, the set command expects one or two arguments.
set x 12
assigns the string "12" (arg2) to the string `x' (arg1).
set myfile worldchampion
assigns the string `worldchampion' to the variable `myfile'.
set x [expr 3 + [string length $myfile]]
Here, two command substitutions take place, first executing the Tcl-command string with option length which counts the number of the characters in the file represented by the variable $myfile. Next the command expr adds that number to `3'. To that end the expr will convert and evaluate strings that can be considered as numbers. The option `+' selects the `add' operation of expr. As a result x is set to `16'.
Double quotes allows the argument to have white spaces or other separation characters.
Example:
set var hello world
will give the error message wrong # args: should be "set varName ?newValue?" because the Tcl-parser reads three arguments while it expects only one or two.
set var "hello world"
will work fine.
Quoting a parameter with curly braces delays evaluation of the called command and it groups words together into a single argument.
Example:
set s image_file
set message {The name of the file is $s}
will print The name of the file is $s.
set message "name of the file is $s"
will print The name of the file is image_file.
With the `/' special characters such as `[` or `$' or the newline sequence `/n' can be treated verbatim, without being treated specially.
Example:
set message "US /$ /nUK /£"
returns
US $
UK £
Another example:
set a 3
set b 4
set message "According to \"Pythagoras\" c=\
sqrt($a*$a+$b*$b) = [expr sqrt($a*$a+$b*$b)]"
where the terminating `/' suppresses the newline command being invoked. The result is:
=> According to "Pythagoras" c= sqrt(3*3+4*4) = 5.0