Read an Excerpt
Tcl/Tk
A Developer's Guide
By Clif Flynt
Morgan Kaufmann
Copyright © 2012 Elsevier, Inc.
All right reserved.
ISBN: 978-0-12-384718-8
Chapter One
Tcl/Tk Features
Your first question is likely to be "What features will Tcl/Tk offer me that other languages won't?" This chapter gives you an overview of the Tcl/Tk features. It covers the basics of what Tcl/Tk can offer you and what its strengths are compared with several alternatives.
Tcl is a multifaceted language package. You can use Tcl as a command scripting language, as a powerful multi-platform interpreted language, as a rapid prototyping platform, or as the backbone of an application that is primarily written in C or Java. You can use the Tcl and Tk libraries to embed powerful scripting capabilities into a compiled application like C, Java or FORTRAN. Tcl's simple syntax makes single-use scripts (to replace repetitive command typing or graphical user interface (GUI) clicking) quick and easy to write. Tcl's modularization, encapsulation and object oriented features help you develop large (100,000Clines of code) projects. Tcl's extensibility makes it easy to use Tcl as the base language across a broad range of projects, from machine control to database applications to electronic design applications, network test devices, and more.
Tcl is both free software and a commercially supported package. The core Tcl language is primarily supported by a worldwide group of volunteers. Commercial support can be purchased from ActiveState, Noumena Corporation, Cygnus Solutions, and others.
The central site for Tcl/Tk information is http://www.tcl.tk. The source code repository and some binary snapshots are maintained at http://sourceforge.net/projects/tcl/. Tcl/Tk runtime packages are included with the Linux and FreeBSD packages, and with commercial UNIX distributions such as Solaris, HPUX and Mac OS X. The current binaries for selected systems (including MS-Windows, Linux, Mac OS X, and Solaris) are available from ActiveState (http://www.activestate.com).
One of the strengths of Tcl is the number of special-purpose extensions that have been added to the language. Extensions are commonly written in "C" and add high-performance special purpose features that not all users need to the language. The use of extensions keeps the Tcl base language small, while supporting a wide variety of uses.
The most popular Tcl extension is Tk, which stands for Tool Kit. This extension provides tools for graphics programming, including drawing canvases, buttons, menus, and so on. The Tk extension is considered part of the Tcl core and is included in the source code distributions at Sourceforge and most binary distributions.
Other popular extensions to Tcl include [incr Tcl] (which adds support for C++ style object-oriented programming to Tcl) and expect, an extension that simplifies controlling other applications and devices and allows many complex tasks to be easily automated.
With Tcl 8.6 (released in 2010) object-oriented support is part of the core language. The Tcl-OO support provides object-oriented features that mimic smalltalk, Objective-C and C++. Tcl-OO is a powerful programming tool, and can be used to create pure Tcl extensions like [incr Tcl] and XOTcl. Creating these language extensions in pure Tcl, without any compiled code, assures that the extension will run on any platform that the Tcl interpreter can be built for.
Many extensions including expect (for controlling remote systems and routers), TclX (with commands for systems administration), Img and crimp (for image processing) are available both as source code and in binary format. Links to these resources are available from the companion website by visiting: http://www.elsevierdirect.com/9780123847171.
Dr. John Ousterhout received the 1997 Software System Award from the Association for Computing Machinery (ACM) for inventing Tcl/Tk. This award recognizes the development of a software system that has a lasting influence. The ACM press release says it well.
The Tcl scripting language and its companion Tk user interface toolkit have proved to be a powerful combination. Tcl is widely used as the glue that allows developers to create complex systems using preexisting systems as their components. As one of the first embeddable scripting languages, Tcl has revolutionized the creation of customizable and extensible applications. Tk provides a much simpler mechanism to construct graphical user interfaces than traditional programming languages. The overall Tcl/Tk system has had substantial impact through its wide use in the developer community and thousands of successful commercial applications.
1.1 TCL OVERVIEW
Tcl (pronounced either as the three letters, or as "tickle") stands for Tool Command Language. This name reflects Tcl's strength as a scripting language for gluing other applications together into a new application.
Tcl was developed by Dr. John Ousterhout while he was at the University of California at Berkeley. He and his group developed simulation packages that needed macro languages to control them. After creating a few on-the-fly languages that were tailored to one application and would not work for another, they decided to create an interpreter library they could merge into the other projects. This provided a common parsing package that could be used with each project, and a common base language across the applications.
The original design goal for Tcl was to create a language that could be embedded in other programs and easily extended with new functionality. Tcl was also designed to execute other programs in the manner of a shell scripting language. By placing a small wrapper around the Tcl library, Dr. Ousterhout and his group created tclsh, a program that could be used as an interactive shell and as a script interpreter.
Dr. Ousterhout expected that this solid base for building specialized languages around a common set of core commands and syntax would be useful for building specialized tools. However, as programmers created Tcl extensions with support for graphics, database interaction, distributed processing, and so on, they also started writing applications in pure Tcl. In fact, the Tcl language turned out to be powerful enough that many programs can be written using tclsh as an interpreter with no extensions.
Today, Tcl is widely used for in-house packages, as an embedded scripting language in commercial products, as a rapid prototyping language, as a framework for regression testing, and for 24/7 mission-critical applications. The robustness of the Tcl interpreter is demonstrated by such mission-critical applications as controlling offshore oil platforms, product Q/A and Q/C operations, and running the NBC broadcasting studios. Many companies are open about their use of Tcl and many more consider Tcl their secret competitive edge.
1.1.1 The Standard Tcl Distribution
Tcl is usually distributed with two interpreters (tclsh and wish) , documentation and support libraries. Tclsh (pronounced as ticklish) is a text-based interpreter and wish is that basic interpreter with Tk graphics commands added.
You can use the Tcl interpreter (tclsh) as you would use UNIX shell scripts or MS-DOS batch (.bat) scripts to control and link other programs or use wish to create GUI interfaces to these scripts. The tclsh interpreter provides a more powerful environment than the standard UNIX shell or .bat file interpreters.
The Tcl documentation facility integrates into the native look and feel of the platform Tcl is installed on.
1.1.2 Documentation
On a UNIX/Linux platform, the man pages will be installed under installationDirectory/ man/mann, and can be accessed using the man command. You may need to add the path to the installed manual pages to your MANPATH environment variable.
On Microsoft Windows platforms, you can access the Tcl help from the Start menu, shown in the following illustration.
This will open a window for selecting which help you need. The window is shown in the following illustration.
Selecting Tcl Manual/Tcl Built-In Commands/List Handling from that menu will open a window like that shown in the following illustration. You can select from this window the page of help you need.
A Macintosh running Mac OS X comes with Tcl/Tk installed. You can access the man pages by opening a terminal window and typing man commandName as shown in the following illustration.
If you install a non-Apple version of the tclsh and wish interpreters (for instance whatever the latest ActiveState release is), the new man pages will be installed in a location defined by the installation.
The following image shows a Safari view of the ActiveState html man pages for Tcl 8.6.
1.2 TCL AS A GLUE LANGUAGE
A command glue language is used to merge several programs into a single application. UNIX programmers are familiar with the concept of using the output from one program as the input of another via pipes. With a glue language, more complex links between programs become possible. Instead of the linear data flow of a set of piped programs, you can have a tree-like data flow in which several sets of data flow into one application. For example, several programs that report network behavior can be merged with a glue language to create a network activity monitor.
There are good reasons to use simple glue language scripts in your day-to-day computing work.
• A script can glue existing programs into a new entity. It is frequently faster to glue together several small programs to perform a specific task than it is to write a program from scratch.
• A script can repeat a set of actions faster and more reliably than you can type. This is not to disparage your typing skills, but if you have to process 50 files in some consistent manner it will be faster to type a five-line script and have that loop through the file names than to type the same line 50 times.
• You can automate actions that would otherwise take several sets of window and mouse operations. If you have spent much time with GUI programs using Microsoft Windows, Mac OS, or the X Window System, you have probably noticed that there are certain operations you end up doing over and over again, without a hot button you can use to invoke a particular set of button and mouse events. With a scripting language, you can automate a set of actions you perform frequently.
• The script provides a record of commands and can act as procedure documentation.
If you have written scripts using the Bourne shell under UNIX, or .bat files under MS-DOS/MSWindows, you know how painful it can be to make several programs work together. The constructs that make a good interactive user shell don't necessarily make a good scripting language.
Using Tcl, you can invoke other programs, just as you would with shell or .bat scripts, and read any output from those programs into the script for further processing. Tcl provides the string processing and math functionality of awk, sed, and expr without needing to execute other programs. It is easier to write Tcl scripts than Bourne shell scripts with awk and sed, since you have to remember only a single language syntax. Tcl scripts also run more efficiently, since the processing is done within a single executable instead of constantly executing new copies of awk, sed, and so on.
Note that you can only use a script to control programs that support a non-GUI interface. Many GUI-based programs have a command line interface suitable for scripting. Others may support script languages of their own or have a dialog-based mode. Under MS Windows, you can also interact applications using Tcl's DDE and COM extensions.
You can use a wish script as a wrapper around a set of programs originally designed for a text-based user interaction (query/response, or one-at-a-time menu choices) and convert the programs into a modern GUI-based package. This is a very nice way of adding a midlife kicker to an older package by hiding the old-style interactions from users more accustomed to graphical environments.
(Continues...)
Excerpted from Tcl/Tk by Clif Flynt Copyright © 2012 by Elsevier, Inc.. Excerpted by permission of Morgan Kaufmann. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.