Debugging Tcl

by Ed Hurley

Most of what you need to know about Tcl is covered in the Tcl for Web Nerds guide. What you can't find there you can probably find in one of the books recommended in Readings and other documentation. However, in this section we'll cover two ways to debug your Tcl code.

Testing a Tcl program with tclsh

One of the main reasons we use Tcl for web development is that it's an interpreted language. This is a big win when it comes to debugging your code. Tcl comes with a shell named tclsh. tclsh has a man page which you can read by typing "man tclsh" at your Unix prompt. Invoking tclsh will give you a tclsh command prompt at which you type (hopefully valid) tcl expressions which are evaluated for you by tclsh when you hit Return. This is the best way to learn Tcl.

However, the tclsh doesn't have a very good command line editor, which makes typing in procedure definitions somewhat painful. The preferred way to evaluate and debug anything more than a single expression is to use a text editor (e.g. Emacs), to write your procedures. Typically you would save them in a file with a .tcl extension.

You can then load these procedure definitions at the tclsh command prompt with: "source myfilename.tcl". You can also have other Tcl expressions in your file besides procedure definitions. Once you've done this the procedure names are bound to their bodies inside the tclsh, and you can invoke them as you would a built-in tclsh procedure (e.g. puts).

For example, suppose you define a procedure "square" inside the file myfile.tcl. And suppose the procedure takes a single number as argument and returns the square of that number. Then you could do the following:

hurley@webwork1:[/home/hurley]% tclsh
% source myfile.tcl
% square 2

If you discover a bug, you can modify your code inside myfile.tcl and re-source it. This will re-bind the procedure names, effectively overwriting the previous definitions. You can repeat this cycle as often as needed.

Testing a Tcl proc inside AOL Server
Add a comment | Add a link