Chapter 1. Introducing Tcl
The purpose of this chapter is to refresh your memory with some basic and fundamental information about Tcl—what it is, what it is not, and why it was created. It is not intended to make you learn Tcl from scratch or to replace or duplicate the official Tcl documentation—there are a lot of sources, both online and printed. However, it is to briefly summarize facts and allow the reader to easily "tune in" to the topic of the book. If you are new to Tcl, after going through this chapter, you will have an overview of the language and its basics, and it would be a good idea to experiment with it, create your own scripts, and study sophisticated options for the described commands before proceeding to further reading. If you are an experienced Tcl programmer, you can probably skip this chapter.
This chapter discusses the origins of Tcl language—the fundamental idea behind its creation, why it was created, and what kind of problems it addresses. It presents Tcl concepts, describes the possibilities Tcl offers, and explains flexibility that comes from a choice of extensions available for use. We also learn about the license agreement it is distributed under and who is involved in this undertaking.
The chapter also talks about a range of enterprise-level platforms supported by Tcl. Its strength comes from the portability it offers. We briefly describe how to obtain a ready-to-use distribution to start your adventure with Tcl, along with its installation on both Windows and Unix environments.
We also describe the tools you can use to develop your own code. To start doing this, nothing more than a plaintext editor is required. However, you will save lots of time and frustration by using an Integrated Development Environment (IDE). Both Eclipse and Komodo are discussed, along with installation and configuration tips, and the creation and execution of your first script.
After that, we come to Tcl's syntax, giving you the basics of reading the code and creating your first scripts. Once you have read this, you will be able to understand the structure of the code, identify its blocks and particular instructions. You will also get familiar with the basic data types and flow control structures offered by the language.
What is Tcl/Tk
Basically, Tcl is yet another scripting language. It was created by Professor John K. Ousterhout in 1988. Why would someone bother to create their own language? John and his students created a set of tools for designing integrated circuits at the University of California at Berkeley. Many of these tools had their own dedicated command languages, allowing interaction with the user. As the primary focus was on the tools, the command languages were often weakly designed, with odd syntax. With the passing of time, the team realized they were missing an easy-to-use, simple, and embeddable common command language that would allow various tools to have a unified command syntax, and it was only a matter of time before Tcl came into existence.
Consider building a complex application similar to the task of building a house. All you need are bricks of different shapes and functionality, and some kind of substance such as filler or glue to keep it all together. This filler is Tcl, and the bricks are components exposed as commands. What more, you can use bricks (components) provided by someone else (like factory), or you can create your own sophisticated bricks that will do whatever you need them to do, and all these bricks are still joined by the same substance—Tcl! Therefore, an application should be considered as a set of components / modules / "bricks" of different characteristics and purposes, each exposed as a command with standardized syntax and a Tcl script that joins them all. Now when creating your program, you do not have to 'reinvent the wheel' by implementing another to learn the error-prone command language—you can simply embed Tcl inside it. Tcl strongly supports and forces componentization of your application. It allows you to configure and control every application module so that it fits your needs.
Tcl is a scripting language with a UNIX-shell like syntax, where all operations are commands. Yes, let's say that again—it's all about the commands here. Everything in Tcl is a command. It comes with a set of built-in commands that implement basic ideas that every programming language simply has to have, namely variables, flow control, or procedures. But this is where the magic begins, as you will see later in the second chapter, the flexibility of Tcl allows us to easily extend it with object-oriented programming concepts.
Tcl is supported on a wide range of platforms, including Microsoft Windows, Mac OS, Linux, Solaris, HP-UX, AIX and others. You can get a full list of supported platforms at: http://www.tcl.tk/software/tcltk/platforms.html
Tcl is a dynamic scripting language—this means the source code you write using Tcl is not compiled in to binary file that may be executed on its own. On the contrary, it is stored 'as is'—in form of a set of instructions (commands) written in one or more plaintext files, interpreted at runtime by the Tcl interpreter. The interpreter is nothing more than a normal binary application that can understand and process scripts it is provided with. A support for a specific platform means that there is an interpreter for this platform.
Each running Tcl interpreter should be considered as a separate virtual machine having its own internal state that is consequently modified by subsequent commands read from standard input. The commands can also be read from the file, so you can execute your existing script, which will eventually lead the virtual interpreted machine to a new state and then interact with it dynamically.
It is possible to use Tcl entirely in an interactive manner by entering commands manually instead of executing an earlier created script. In order to do this, you simply run the interpreter and enter commands via the simple, standard shell—after the prompt (by default it is % character). This ability adds great flexibility into both experimenting with and rapidly developing the code. This shell application is commonly called tclsh
, but for example, in case of the ActiveTcl distribution (described later in this chapter), the name tclsh85
is used to reflect Tcl version it supports. The following screenshot illustrates the shell's start as well as the execution of some basic commands:
The Tcl command info patchlevel
returns Tcl's version information and exit
terminates the interactive interpreter shell.
As Tcl scripts are nothing more than plaintext files, you do not need any special tools to develop them. All you need is a text editor with which you can create or modify Tcl files. If tclsh
is executed with the filename, the shell is not shown, and the interpreter executes the script that is read from that file. The following screenshot illustrates this:
The script file called main.tcl
constitutes the entire script in this example. Its content is the very much used "Hello World" example:
#my first Tcl script puts "Hello World one more time!"
The first line demonstrates how comments are made in Tcl—it starts with the #
character, and the second line contains the command put
with the string attribute enclosed within quotation marks. This command will send the provided string to standard output.
To execute it, we pass the file to Tcl interpreter tclsh85
.
Along with tclsh
comes another application called wish
, which basically has the same functionality. The main difference is that it preloads the Tk package and, in case of MS Windows, creates a separate window-based shell rather than using the system console.
Early implementations of Tcl interpreters were internally based on string representation of executed script that resulted in sluggish execution and poor speed. In later versions, a "just-in-time" compiler was added, which converts the Tcl script, on the fly, into the byte code language (BCL) internal representation—a concept used in languages such as Java. Such a byte code representation of the script is next executed by the interpreter, which significantly improves its performance. It is also possible to save compiled script for future usage, with the typical extension .tbc
. The main benefit to using bytecode files is that the source code is hidden—it does not give any real performance improvements.
Based on what we discussed till now, any Tcl program consists of one or more scripts plus an interpreter. Such a form allows easy and rapid development, meaning that you simply need to modify one of your scripts with text editor, but it also has its own drawbacks. It is much easier to distribute your program as a standalone, ready-to-execute binary file. Distribution of Tcl source files would be really awkward—for example, you may be not able to install the interpreter because of your system administrator's policy. Besides, how many times have you wanted to try out some interesting Java application only to get angry when you realized that to run it, you have to download dozens of megabytes and install Java virtual machine? If you consider this a "not so elegant" solution and are in favor of standalone applications, here is the good news—it is possible to pack all your scripts along with the interpreter into one binary, executable file that is not different from "normal" applications! We will discuss this in more detail in Chapter 3. It is quite possible that you are already an unknowing user of such applications, as there are a number of these available, for example, an excellent MP3 player called SnackAmp (http://snackamp.sourceforge.net) or the encrypted passwords vault Password Gorilla (http://www.fpx.de/fp/Software/Gorilla/).
Tcl language is a standard example of an open source idea. The BSD-style license gives you all the freedom of usage; you can use and modify it in any way you want—also, commercial usage is permitted (detailed license is available at http://www.tcl.tk/software/tcltk/license.html). Both Tcl and Tk are maintained by a world-wide community lead by the group of experts composing Tcl Core Team. Anyone can propose improvements by using the Tcl Improvement Proposal (TIP) feature and sending the proposal to TCT. The community's central site is the Tcl Developer Xchange (http://www.tcl.tk/), which serves as a primary source of Tcl-related information.
One of the most significant contributors is a Canadian Software Company called ActiveState, which specializes in dynamic languages. It provides its own distribution of Tcl called ActiveTcl and also offers various development tools (both of these are discussed later in this chapter).
Across this book, we will present Tcl in its latest available version, which is 8.5 at the time of writing this book. However, majority of information is valid for the version 8.4 and the previous versions. To sum it all up, Tcl is a mature scripting language with a lot of extensions coming from the open source community, and it allows you to quickly develop multi-platform command-line or GUI-based applications.
Extensions
The great flexibility of Tcl comes from its ability to extend the available set of commands known to the interpreter by adding additional extensions, that is, Tcl packages. Packages can be written in pure Tcl, making them platform-independent, or in native code like C/C++, which is then compiled for a specific target platform. It is possible that a package will consist of both of these. There are a lot of packages available on the Internet, both free of use and commercial. A good place to start is Tcllib—Tcl Standard Library. This is a set of extensions written in pure Tcl, available at http://tcllib.sourceforge.net/. Soon you will notice that we will use various packages from this repository for the examples in this book.
What's more interesting is that if you find out that nobody created an extension you would like to use, you will be able to create your own one and share it with the community.
Tk
One of the extensions this chapter will cover is Tk, which was developed by John Osterhout. Tk is a cross-platform GUI framework, which provides the same functions for Tcl as AWT and Swing do for Java. In other words, it is a library that contains a set of typical basic elements (widgets) such as buttons, frames, a canvas, menus, and so on. Tk widgets are flexible in terms of their customization options; almost every aspect may be defined at time of creation or modified later. One of its features is a native "look and feel", which simply means that Tcl/Tk graphical applications do not differ significantly from other native applications in terms of the look and usage. As over the years, Tk appearance started to fall behind the looks of modern operating systems, a number of extensions were created. Also worth mentioning is the Tile Widget Set (http://tktable.sourceforge.net/tile/), which brings re-implementation of some Tk core widget, as well as addition of a few new widgets. Tile is also an engine that allows the usage of themes. According to the Tile documentation:
A theme is a collection of elements and styles that determine the look and feel of the widget set.
Tile has been incorporated into Tk core functionality in version 8.5, which means it is now available in all Tcl/Tk distributions. Although many Tk widgets have corresponding newer Tile equivalents, they can coexist in one application and the developer has the freedom of choice as to which version to use.
As an example, have a look at the following screenshot of SnackAmp that illustrates Tcl's possibilities in the field of GUI applications:
Tk became so popular that it is no longer Tcl-specific. It is now possible to use it in other scripting languages like Perl or Python.