Tcl (programming language)
Tcl is a high-level, general-purpose, interpreted, dynamic programming language. It was designed with the goal of being very simple but powerful. Tcl casts everything into the mold of a command, even programming constructs like variable assignment and procedure definition. Tcl supports multiple programming paradigms, including object-oriented, imperative, functional, and procedural styles.
It is commonly embedded into C applications for rapid prototyping, scripted applications, GUIs, and testing. Tcl interpreters are available for many operating systems, allowing Tcl code to run on a wide variety of systems. Because Tcl is a very compact language, it is used on embedded systems platforms, both in its full form and in several other small-footprint versions.
The popular combination of Tcl with the Tk extension is referred to as Tcl/Tk and enables building a graphical user interface natively in Tcl. Tcl/Tk is included in the standard Python installation in the form of Tkinter.
History
The Tcl programming language was created in the spring of 1988 by John Ousterhout while he was working at the University of California, Berkeley. Originally "born out of frustration", according to the author, with programmers devising their own languages for extending electronic design automation software and, more specifically, the VLSI design tool Magic, which was a professional focus for John at the time. Later Tcl gained acceptance on its own. Ousterhout was awarded the ACM Software System Award in 1997 for Tcl/Tk.The name originally comes from "Tool Command Language", but is conventionally written Tcl rather than TCL.
| Date | Event |
| January 1990 | Tcl announced beyond Berkeley. |
| June 1990 | Expect announced. |
| January 1991 | First announcement of Tk. |
| June 1993 | First Tcl/Tk conference. |
| August 1997 | Tcl 8.0 introduced a bytecode compiler and namespaces. |
| April 1999 | Tcl 8.1 introduces full Unicode support and advanced regular expressions. |
| August 1999 | Tcl 8.2 introduces Tcl Extension Architecture |
| August 2000 | Tcl Core Team formed, moving Tcl to a more community-oriented development model. |
| September 2002 | Ninth Tcl/Tk conference. Announcement of starkit packaging system. Tcl 8.4.0 released. |
| December 2007 | Tcl 8.5 added new datatypes, a new extension repository, bignums, lambdas. |
| December 2012 | Tcl 8.6 added built-in dynamic object system, TclOO, and stackless evaluation. |
| September 2024 | Tcl 9.0 added 64-bit capabilities, support for the full Unicode code point range, uses epoll & kqueue |
Tcl conferences and workshops are held in both the United States and Europe. Several corporations, including FlightAware use Tcl as part of their products.
Features
Tcl's features include- All operations are commands, including language structures. They are written in prefix notation.
- Commands commonly accept a variable number of arguments.
- Everything can be dynamically redefined and overridden. Actually, there are no keywords, so even control structures can be added or changed, although this is not advisable.
- All data types can be manipulated as strings, including source code. Internally, variables have types like integer and double, but converting is purely automatic.
- Variables are not declared, but assigned to. Use of a non-defined variable results in an error.
- Fully dynamic, class-based object system, TclOO, including advanced features such as meta-classes, filters, and mixins.
- Event-driven interface to sockets and files. Time-based and user-defined events are also possible.
- Variable visibility restricted to lexical scope by default, but
uplevelandupvarallowing procs to interact with the enclosing functions' scopes. - All commands defined by Tcl itself generate error messages on incorrect usage.
- Extensibility, via C, C++, Java, Python, and Tcl.
- Interpreted language using bytecode
- Full Unicode support, first released 1999.
- Regular expressions
- Cross-platform: Windows API; Unix, Linux, Macintosh etc.
- Close, cross-platform integration with windowing interface Tk.
- Multiple distribution mechanisms exist:
- * Full development version
- * , and
- * , a small footprint Tcl implementation
- * Freely distributable source code under a BSD license.
Safe-Tcl
Syntax and fundamental semantics
The syntax and semantics of Tcl are covered by twelve rules known as the Dodekalogue.A Tcl script consists of a series of command invocations. A command invocation is a list of words separated by whitespace and terminated by a newline or semicolon. The first word is the name of a command, which may be built into the language, found in an available library, or defined in the script itself. The subsequent words serve as arguments to the command:
commandName argument1 argument2... argumentN
The following example uses the puts command to display a string of text on the host console:
puts "Hello, World!"
This sends the string "Hello, World!" to the standard output device along with an appended newline character.
Variables and the results of other commands can be substituted into strings, such as in this example which uses the set and expr commands to store the result of a calculation in a variable, and then uses puts to print the result together with some explanatory text:
- expr evaluates text string as an expression
puts "The sum of the numbers 1..5 is $sum."
The
# character introduces a comment. Comments can appear anywhere the interpreter is expecting a command name.- with curly braces, variable substitution is performed by expr
set sum ; # $x is not substituted before passing the parameter to expr;
# expr substitutes 1 for $x while evaluating the expression
puts "The sum of the numbers 1..5 is $sum."; # sum is 15
- without curly braces, variable substitution occurs at the definition site
set op *
set y 3
set res ; # $x, $op, and $y are substituted, and the expression is evaluated to 6
puts "$x $op $y is $res."; # $x, $op, $y, and $res are substituted and evaluated as strings
As seen in these examples, there is one basic construct in the language: the command. Quoting mechanisms and substitution rules determine how the arguments to each command are processed.
One special substitution occurs before the parsing of any commands or arguments. If the final character on a line is a backslash, then the backslash-newline combination are replaced by a single space. This provides a line continuation mechanism, whereby long lines in the source code can be wrapped to the next line for the convenience of readers.
Continuing with normal argument processing, a word that begins with a double-quote character extends to the next double-quote character. Such a word can thus contain whitespace and semicolons without those characters being interpreted as having any special meaning. A word that begins with an opening curly-brace character. Inside curly braces all forms of substitution are suppressed except the previously mentioned backslash-newline elimination. Words not enclosed in either construct are known as bare words.
In bare and double-quoted words, three types of substitution may occur:
- Command substitution replaces the contents of balanced square brackets with the result of evaluating the script contained inside. For example,
is replaced by the result of evaluating the contained expression. - Variable substitution replaces the name of a variable prefixed with a dollar sign with the contents of the variable. For example,
$foois replaced by the contents of the variable called "foo". The variable name may be surrounded by curly braces to separate it from subsequent text in otherwise ambiguous cases. - Backslash substitution replaces a backslash followed by a letter with another character. For example,
\nis replaced by a newline.
From Tcl 8.5 onwards, any word may be prefixed by
, which causes the word to be split apart into its constituent sub-words for the purposes of building the command invocation.As a consequence of these rules, the result of any command may be used as an argument to any other command. Note that, unlike in Unix command shells, Tcl does not reparse any string unless explicitly directed to do so, which makes interactive use more cumbersome, but scripted use more predictable.
The single equality sign serves no special role in the language at all. The double equality sign is the test for equality which is used in expression contexts such as the
expr command and in the first argument to if. The majority of Tcl commands, especially in the standard library, are variadic, and the
proc allows one to define default values for unspecified arguments and a catch-all argument to allow the code to process arbitrary numbers of arguments.Tcl is not statically typed: each variable may contain integers, floats, strings, lists, command names, dictionaries, or any other value; values are reinterpreted as other types on demand. However, values are immutable and operations that appear to change them actually just return a new value instead.