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 

Comments

Balvinder at 15 Aug, 2007 09:20
HI!!!
I need your help,we want TCL sript for Simulation of DSR for 6 nodes...
Please See if you can do something for me,its really urgent,I have to submit it...
Looking forward to you..
Reply to this.
Anonymous at 23 Jul, 2008 03:36
i can help u :) get me at: lowraider1@gmail.com
Reply to this.
kishore at 31 Oct, 2007 11:02
How to execute a TCL/TK program in linux ?????
Reply to this.
Binny V A at 01 Nov, 2007 04:46
Try 'wish tcl_file.tcl'
Reply to this.
Anonymous at 04 Mar, 2009 11:50
simply go to terminal and type gedit then type any program in that then go to terminal type tclsh then %this sign appear then type source foo.tcl(where foo.tcl is the file name that u make in gedit)
Reply to this.
Anonymous at 01 Jul, 2008 08:49
These statements about braces suppressing command and/or variable substitution--do they apply only in the context of the "set" command?

For instance, in the code "while { $i <= 10}" variable substitution is NOT suppressed, right?

I'm not trying to be picky, just trying to make sure I understand. Thanks for your tutorial! --Steve
Reply to this.
Frederick at 07 Sep, 2008 03:28
is this for python aswell?
Reply to this.
Anonymous at 07 Nov, 2008 08:50
Can I get a sample code which shows me how to use 'expect' and tcl?

I want to read/write to a serial port and at the same time, write the received data to a log file.

I also want to know how to configure it using fconfigure.

Thanks in advance
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed