Version 5 of An Interactive Approach to Experimenting with Tcl

Updated 2003-03-31 15:21:32

Purpose: Discuss the use of Tcl in an interactive manner. Pointers to relevant pages and tools are also welcome here.


Tcl can be used in several ways. This page talks about the user of Tcl in an interactive manner. Other pages [fill in some names here] discuss the use of Tcl with files, command lines, in web applications, etc.

So why would anyone ever use Tcl interactively? Tcl is an interpreter, and as such, interacting with it allows one to experiment with the language, as well as allows one to solve some kinds of simple problems.

So, how do you use Tcl in this way?

First, you have to find a Tcl interpreter. These can come named in various ways. A few of the more common ones are:

  • tclsh (This interpreter is usually the one generated when the Tcl source code distribution as distributed by the TCT is built)
  • wish (Tcl interpreter with the Tk extension initialized)
  • expect (Tcl interpreter with the Expect extension initialized)
  • tclkit (Tcl interpreter with Tk, incr Tcl, tclVFS, and Metakit bundled in)
  • bltsh (Tcl interpreter with Tk and BLT initialized]
  • tcldomsh (Tcl interpreter with TclDOM available)
  • tixwish (Tcl interpreter with Tk and Tix initialized)
  • tcl (Tcl interpreter with TclX initialized - this one is no longer being generated)

Next, you execute the Tcl interpreter. If you do this from a command line text type window, you will see a prompt of some kind - what kind depends on the interpreter. (Tcl typically shows a % sign). To assure yourself that you are in Tcl, type:

 info patchlevel

[someone feel free to replace this command with something else uniquely Tclish]

and you should get the version of the Tcl being used. Especially when used interactively, errors in Tcl are not catastrophes to be feared, but like little friends that help in learning. Here's an example session (making use of various info subcommands): after the % is my input, in the next line starts the output from that command.

 % info
 wrong # args: should be "info option ?arg arg ...?"
 % info option
 bad option "option": must be args, body, cmdcount, commands, complete, default,
 exists, functions, globals, hostname, level, library, loaded, locals, nameofexec
 utable, patchlevel, procs, script, sharedlibextension, tclversion, or vars
 % info patchlevel
 8.4.1
 % info nameof
 D:/usr/local/bin/tclsh.exe
 % info vars
 tcl_rcFileName tcl_version argv argv0 tcl_interactive auto_oldpath auto_path err
 orCode errorInfo auto_index env tcl_patchLevel argc tcl_libPath tcl_platform tcl
 _library
 % set tcl_platform
 can't read "tcl_platform": variable is array
 % array set tcl_platform
 wrong # args: should be "array set arrayName list"
 % array get tcl_platform
 osVersion 5.0 byteOrder littleEndian machine intel platform windows os {Windows
 NT} user suchrich wordSize 4
 % pwd
 D:/home/suchrich/bld/smart/bld/zvutil/build
 % expr 17+4
 21
 %

If the interpreter initializes Tk, you will also see a small blank window, which represents a Tk toplevel widget known as ".". It looks strange, as it is empty, you can't initially type in it, etc. However, the window represents the place where your subsequent declarations of buttons, etc. will appear once you issue the appropriate manager commands.

[Someone please fill in what someone from a GUI environment needs to do here so that double clicking on a tclsh or wish, etc. does the right thing.]

The Tcl command

  console show

on some platforms - Windows at least (perhaps MacOS as well) displays a Tk window into which one can type commands and see the results.

[To be added - more examples]


See also TkCON - a great interface for interacting with Tcl. Provides the ability to use the arrow keys to go back in the entered history and edit and then reexecute lines, interact with interpreters, etc.


18mar03 jcw - I've been experimenting with another way to make the edit/run cycle very direct. It could be done with periodic polling, but on Linux, there's also a neat utility called "dnotify" which will run a specified command whenever a change is detected in a certain directory. Dnotify actually hooks into the kernel to respond instantly, so what you can do is issue the following command:

    dnotify -r . -e ./try.tcl

In plain english: whenever a file in the current dir is renamed, run the "try.tcl" script. With this running in one window, you can go to any of the files, edit and then simply save (which renames, at least in "vi"). Each save will refresh the output. Does wonders, as long as the pieces being developed are small and self-contained.


Category Tutorial