Getting Started with Tcl

Getting Started with Tcl

This class uses the Tcl programming language. Tcl is a scripting language that is interpreted at runtime. It has a powerful graphics package (the Tk part of Tcl/Tk) that makes it easy to use for building user interfaces. It also is event drive. We will not be using either of these two aspects of Tcl.

Some features of Tcl

Downloading Tcl

Tcl/Tk is available for a number of platforms, including Windows and Linux. Check out www.tcl.tk. I use the version provided by ActiveState.com, called ActiveTcl, which is available for free. The problem with this version is that it is difficult for beginners to use. When there is a syntax mistake in your program, it doesn't give you good information to help you track down the mistake. Programs can be run in windows by double-clicking them. This will start the wish interpretator, which actually can run programs with both tcl and tk code in them. Below is a sample program to illustrate this.
proc myproc {a b c} {
    puts "The arguments to this program are $a $b $c"
    return 5
}

console show
wm withdraw .

set result [myproc 1 2 3]
puts $result
Note that the `console show' and `wm withdraw .' are a couple of housekeeping commands needed by the wish interpretator. Note that you can type in commands into the wish console. This is a good way to check out how different tcl commands work.

A second option is to run programs from the command line. Here, you should use the tclsh interpretator (in which you cannot run tk commands, just tcl commands).For this option, you need to remove the `console show' and `wm withdraw .' Assuming your program is call hw1.tcl, you would enter the following from the command line `tclsh hw1.tcl'. Of course you might have to indicate the full pathname for tclsh.

There is also no built in debugging facility. Instead, you need to rely on puts statements, and perhaps after 1000 to slow down the rate of messages. Students in past years have used this option.

Using the Tcl Debugger

Another free alternative is to use TclPro debugger. It can be downloaded from www.tcl.tk/software/tclpro/eval/1.4. It does give good syntax error messages, and it can trace through your code. This tool might not support the graphics package of Tcl/Tk, but we won't be using that anyways. We will be just putting the answers to the console. To prevent the console from disappearing after the program has ran, you can stick in the following line of code at the end of your program: vwait junk. I strongly recommend students use this tool.

With TclPro, you can debug your program, but will need to use a separate program to edit it. Each time you change your program (in a separate text editor), you need to press the Refresh button in the File menu. There is a Guide to using TclPro that you get when you download. Note that TclPro works with Tcl8.3. There are more recent versions of Tcl available, so this is one drawback, but it will have little impact on you in this course.

Syntax Quirks of Tcl

Tcl has a lot of quirks in its syntax, and I think the TclPro debugger can help you pinpoint where your syntax errors are. Be very careful with spacing and line breaks. Although most programming languages are lenient about this, not so with Tcl/tk. For instance, the following code is not valid, as there is a new line before the exit
if {$a == "quit"}
   exit
You also need to be careful of comment lines, which start with a #. In general, do not have a `{' or '}' brace in your comment.

Documentation

There is a lot of free information about Tcl.

Here is a site that has man pages of the Tcl commands (from Tcl 8.3): http://tcl.activestate.com/man/tcl8.3/TclCmd/contents.htm.

A good book about Tcl is: Tcl and the Tk toolkit by John Ousterhout, Addison-Wesley, ISBN 0-2010-63337-X.

A good online tutorial is http://xyresix.com/nwsbook/index.html?P=contents. You might want to start at the following search page though: http://xyresix.com/nwsbook/search.html. The author of this site has really great examples, and points out the most common mistakes.

Editing Tcl

For editing Tcl, some people like to use TextPad, which can be used for editing all sorts of programming languages, from Java to C to Tcl to HTML. You can download it from http://www.textpad.com/download.

After you download it, get a syntax file (to make all the keywords pretty colors) for it from the add-ons: http://www.textpad.com/add-ons/synn2t.html. One of my students uses the syntax file TCL/TK (4), but the TCL from 2003 is probably also good - only difference is really the keywords and that's pretty easy to tweak yourself.

Acknowledgments

Kristy Hollingshead and Fan Yang contributed to this page.