The TCL Family of Applications

Posted by: Rea Maor In: Programming - Friday, May 4th, 2007

What’s that? It isn’t the TCL programming language? Well, yes, and no. Tcl (“Tool Command Language”, often pronounced “tickle” and spelled with lowercase) by itself is indeed a language unto itself, but it is actually less useful as a language in it’s own right than it is a framework upon which many more useful tools have been built. Such as:

  • Tk, the “tool kit”. This is a cross-platform widget tool set, making for a pretty accessible GUI system. “Widgets”, if you didn’t know, are the various buttons, sliders, scrollbars, titlebars, icons, radio checkboxes, and various other gizmos you interact with every day on your computer desktop (and even on this web page!) without thinking about it.
  • Wish, the windowing shell. This is actually the framework that implements the Tk widget set. It can make creating a dialog box as simple as a few lines of code.
  • Expect. A really unique tool set in it’s own right. Expect is a language specifically created to script interactions with *other* programs not normally scripted. For instance, the text-mode web browser lynx needs interaction in order to log into a website and hit “y” to accept the cookie, and so on. You can write a script in Expect to automatically drive the lynx web browser, entering user names and passwords and filling out forms and such. This can be used for good or evil, as you are probably thinking about now.

Let’s try to explain this clearly: Tcl is actually a programming language for programs. That is, it’s intended to be embedded. Huh? Let me explain.

Ever played 3D shooters like Quake? If you’ve accessed the console in Quake, you know that the console has it’s own little language. Another example is the extended commands you use with the Google search engine, such as “define:” to look up the definition of a word or “convert X to Y” to convert between units. While not officially named as such, “googlese” is actually a small language to communicate with Google’s search engine.

There are many more examples of this beyond surface interfaces. Any sufficiently sophisticated program needs its own internal language, even if it’s not always thought of as such. Every time you make your own function, you have extended the language to suit some custom need. If you do this a lot, you might create your own libraries. More sophisticated functions will need parameters passed to them, and since it’s all interior to your program, it’s up to you what format and syntax to use. Still another example is our object-oriented examples from C++: Objects have “private” and “public” properties. The “public” properties are to be freely accessed by other objects… and lo and behold, before long you have an internal language that you’ve created for your objects to talk to each other in.

And so, for large programs and software systems, we end up inventing our own mini-languages again and again. Particularly in the case of CGI scripting, which is the programs running on web servers that handle communications between various script elements embedded in web pages.

And so, in the designer’s own words, Tcl was designed “out of frustration” with programmers taking off and designing each their own, non-standard, undocumented embedded languages. Tcl is there to be a pre-built tool for most general purposes, and it has modules that plug in to talk to other languages such as C++, Java, and SQL databases. The Tk extension can also drop easily into Perl, Python, Ruby, and just about every language commonly used. The main benefit to this is that programmers can easily view and modify each other’s code; in large projects with many developers, Java programmers can review the code in the Perl module and at least understand the parts written in Perl/Tk for their Java code to talk to, even if they don’t know Perl itself.

We’ll bypass the standard code listing of each language function, since, like pure object programming in C++, there’s not much here that is immediately useful for the novice. However, here’s some example listings done in Tcl/Tk/wish itself, both running on Linux:

Listing 1: A file finder utility.


#!/usr/bin/wish

proc search {} {
set topic [.ent get]
set result [exec locate -i $topic]
.txt insert end "$result"
}

entry .ent
button .but -text "find it" -command "search"
button .ext -text "quit" -command "exit"
frame .textarea
text .txt -width 80 -height 40 \
-yscrollcommand ".srl_y set" -xscrollcommand ".srl_x set"
scrollbar .srl_y -command ".txt yview" -orient v
scrollbar .srl_x -command ".txt xview" -orient h
pack .ent
pack .but
pack .ext
grid .txt -in .textarea -row 1 -column 1
grid .srl_y -in .textarea -row 1 -column 2 -sticky ns
grid .srl_x -in .textarea -row 2 -column 1 -sticky ew
pack .textarea

This is just a GUI front-end to the Unix “locate” command, which works very much like any desktop file-finder utility. Here’s a shot of the program in action:

find box example

Listing 2: A Fluxbox wallpaper-changer.


#!/usr/bin/wish

proc set_BG {} {
global att
set BG [tk_getOpenFile -initialdir ~/wallpapers/]
if {$BG != ""} {
exec fbsetbg -$att $BG
set outfile [open "~.myBG" w]
puts $outfile $BG
close $outfile
}
}

set att "f"

label .instr -text "pick a background from the menu:"
pack .instr

radiobutton .att_f -text "full" -variable att -value "f"
radiobutton .att_c -text "centered" -variable att -value "c"
radiobutton .att_t -text "tiled" -variable att -value "t"
radiobutton .att_a -text "aspect" -variable att -value "a"
pack .att_f .att_c .att_t .att_a

button .get -text "pickBG" -command "set_BG"
pack .get

This is a front-end for the Fluxbox desktop background setter, fbsetbg. It replaces the call to the user’s wallpaper folder and various command switches with a few mouse-clicks. Here’s a shot of it in action:

Fluxbox background tool

As you can see, the “wish” windowing shell is a very handy way to glue a graphical user interface onto a command line program. While it isn’t a show-stopper in the looks department, it is great for simple proto-typing or running up a quick utility. This is especially handy when you’re administering a system for command-line shy users who are more comfortable with mouse clicks than remembering complex commands. It combines easily with Bash shell scripts.

Tcl/tk is ported to nearly every platform in existence, including Windows, Mac, and Unix, and available as plug-ins for practically every programming language under the sun (including the ones that come from Sun microsystems!). Let’s just close with some helpful links:

Tcl Developer exchange – get info and downloads.

ActiveTcl – A commercial Tcl, also for all platforms. Download is free.

Tcl for Web Nerds – An MIT-affiliated web book illustrating web applications of Tcl

Puppy Linux – A live Linux CD with most of the desktop tools built in Tcl/Tk/wish! Worth a spin just for the source code. Puppy Linux is a small 60MB live CD which can run entirely in RAM, and also makes a great utility and rescue CD.

The Expect Wiki – just please don’t go off and use this as a bot to wreck havoc on the Internet, OK? This thing’s almost too dangerous to show people.

Our next visit is to another quirky language with a peculiar personality, and then we’ll be back to more widely deployed web languages…


Related Posts:


Leave a Reply