Version 2 of An Interactive Approach to Experimenting with Tcl

Updated 2003-03-19 18:29:37

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 ".".

[Someone please fill in what someone from a GUI environment needs to do here.]

[To be discussed - use of console show on some platforms (and not others)]

[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.


Category Tutorial