Tcl TutorialIntroductionThis is a very short introduction to the Tcl script language. If you just can't wait, I hope this will make you able to read and to understand simple Tcl code. In many points, Tcl is similar to C, especially for loop structures, function definitions and mathematical or conditional expressions. In other points, such as expression evaluation and list data structures, you will notice that Tcl has inherited from the benefits of the Scheme language. In Tcl, all data is represented as strings. Commands evaluationEach Tcl command call is a sentence of the form : command arg1 arg2 arg3 ... The Tcl evaluator take each word of this sentence and evaluate it. After evaluation of each word, the first word (command) is considered to be a function name and this function is executed with as arguments the following words. To evaluate a word, the interpretor has to do the following substitutions in the word string :
Examples
If you want to break a command sentence in lines you can only do it inside the { } brace or in the " " quotation or you can break the line with a '\' at the end of any break line. Example
This test the value of the variable c. If it is the string 'Hell' it prints 'Oh god !' on screen, otherwise, it prints 'Peace !'. In this sentence, Tcl see 5 words :
The first word, 'if' is seen as the command and this command is executed with as parameters the 4 following words. That is later that the condition '$c == "Hell"' is evaluated, during the execution of the if command. Notice where we placed the line breaks (inside the { }). Strings and ListsUnder Tcl, the value of each variable is stored as a string. Even if you want to save a number in a variable, this number is transformed into a string. As a special type of string, the list deserve a special attention in data representation in Tcl. The list is nothing more than a string with, as elements separator, the space. A list may contains sublists. Example
Mathematics expressionWhereas all variables are of type string, the mathematical operations internally uses float and integer number representation to produce their results. The command that calculate mathematical expression is 'expr'. Example
In the first calculation, the interpretor has used the integer number representation. In the second, it has used the float number representation. How to display something ?To display a string, you can use the command 'puts' Example
As it can be seen in the previous example, the command format is very similar to the C command 'printf'. Control flowThe following commands are similar to the C equivalent. Only 'foreach' has no C equivalent (have a look at the example to see what it do).
Examples
ArrayArrays are always unidimensional but the index is a string. If you use a separator in the index string (such as ',', '-'), you can get the same effect than with a multidimensional array in other languages. Example
ProceduresProcedures are the equivalent of the C functions. Example
if a procedure does not contain any 'return' statement, the default return value is the return value of the last evaluated function in this procedure. So the following script is perfectly equivalent :
To call the 'sum2' function, we do the following :
The special argument name 'args' contains a list with the rest of the arguments Example
it is also possible to specify default parameters. So, if you don't specify
the last parameters, the default values will be substituted.
Example
If you want to use global variables in a function, you have to declare it as global. Example
You can also declare a table as global. Example
EvalThe 'eval' command
In the following example, we used the function 'sum' that we have already defined. Example
If you had omitted the 'eval' command in the previous example, the 'sum' procedure would have returned an error because 'sum' should be called with only one string argument (in the previous example, this argument would have been '45.0 65.0 78.0 55.0') while 'sum' is expecting numerical arguments. uplevel, upvarWith the 'upvar' command, you can access a variable which belongs to a higher level of the procedure call stack. Example
In the previous example, the parameter 'n' gets the value 'nb' (the string 'nb' !) if we type 'decr nb 3'. The command 'upvar $n upa' means that the variable 'upa' becomes a synonym to the variable 'nb' (coming from a higher level of the stack). With the 'uplevel' command, you can evaluate something on higher level in the stack. Example
Inside the procedure 'do', the evaluation of the script 'todo' and the conditional 'condition' has to made on a higher level of stack (in the same way that if they were evaluated from out of 'do'). error and catchIf you insert an 'error' command in your code, this command will stop the execution of your script and return the error message that follow the 'error' command. With the command 'catch', you can also intercept a error to avoid that your script stops on an error. If 'catch' return 0, it means that no error occurred while evaluating the script send as parameter of catch. If 'catch' return 1, it means that an error occurred. Example
The last call is completely equivalent to
|