Tcl


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 and functional programming or procedural styles.
It is commonly used 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 working at the University of California, Berkeley. Originally "born out of frustration", according to the author, with programmers devising their own languages intended to be embedded into applications, 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 spelled "Tcl" rather than "TCL".
DateEvent
January 1990Tcl announced beyond Berkeley.
June 1990Expect announced.
January 1991First announcement of Tk.
June 1993First Tcl/Tk conference. geometry manager, incr Tcl|, TclDP and Groupkit, announced there.
August 1997Tcl 8.0 introduced a bytecode compiler.
April 1999Tcl 8.1 introduces full Unicode support and advanced regular expressions.
August 1999Tcl 8.2 introduces Tcl Extension Architecture
August 2000Tcl Core Team formed, moving Tcl to a more community-oriented development model.
September 2002Ninth Tcl/Tk conference. Announcement of starkit packaging system. Tcl 8.4.0 released.
December 2007Tcl 8.5 added new datatypes, a new extension repository, bignums, lambdas.
December 2012Tcl 8.6 added built-in dynamic object system, TclOO, and stackless evaluation.

Tcl conferences and workshops are held in both the United States and Europe.

Features

Tcl's features include
Safe-Tcl is a subset of Tcl that has restricted features so that Tcl scripts cannot harm their hosting machine or application. File system access is limited and arbitrary system commands are prevented from execution. It uses a dual interpreter model with the untrusted interpreter running code in an untrusted script. It was designed by Nathaniel Borenstein and Marshall Rose to include active messages in e-mail. Safe-Tcl can be included in e-mail when the application/safe-tcl and multipart/enabled-mail are supported. The functionality of Safe-Tcl has since been incorporated as part of the standard Tcl/Tk releases.

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:

  1. expr evaluates text string as an expression
set sum
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.

  1. with curly braces, variable substitution is performed by expr
set x 1
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


  1. without curly braces, variable substitution occurs at the definition site
set x 2
set op *
set y 3
set res ; # $x, $op, and $y are substituted, and the expression is evaluated
puts "2 * 3 is $res."; # 6 is substituted for $res

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:
Substitution proceeds left-to-right in a single scan through each word. Any substituted text will not be scanned again for possible further substitutions. However, any number of substitutions can appear in a single word.
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. Also, there is no operator or command for string concatenation, as the language concatenates directly. 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.

Basic commands

The most important commands that refer to program execution and data operations are:
The usual execution control commands are:
Those above looping commands can be additionally controlled by the following commands:
uplevel allows a command script to be executed in a scope other than the current innermost scope on the stack. Because the command script may itself call procedures that use the uplevel command, this has the net effect of transforming the call stack into a call tree.
It was originally implemented to permit Tcl procedures to reimplement built-in commands and still have the ability to manipulate local variables. For example, the following Tcl script is a reimplementation of the for command :

proc for

Upvar

upvar arranges for one or more local variables in the current procedure to refer to variables in an enclosing procedure call or to global variables. The upvar command simplifies the implementation of call-by-name procedure calling and also makes it easier to build new control constructs as Tcl procedures.
A decr command that works like the built-in incr command except it subtracts the value from the variable instead of adding it:

proc decr

Object-oriented

Tcl 8.6 added a built-in dynamic object system, TclOO, in 2012. It includes features such as:

oo::class create fruit
oo::class create banana
set b
$b eat → prints "skin now off" and "yummy!"
fruit destroy
$b eat → error "unknown command"

Tcl did not have object oriented syntax until 2012, so various extension packages emerged to enable object-oriented programming. They are widespread in existing Tcl source code. Popular extensions include:
TclOO was not only added to build a strong object oriented system, but also to enable extension packages to build object oriented abstractions using it as a foundation. After the release of TclOO, incr Tcl was updated to use TclOO as its foundation.

Web application development

is a pure-Tcl implementation of an HTTP protocol server. It runs as a script on top of a vanilla Tcl interpreter.
is an open source programming system for Apache HTTP Server that allows developers to use Tcl as a scripting language for creating dynamic web applications. Rivet is similar to PHP, ASP, and JSP. Rivet was primarily developed by Damon Courtney, David Welton, Massimo Manghi, Harald Oehlmann and Karl Lehenbauer. Rivet can use any of the thousands of publicly available Tcl packages that offer countless features such as database interaction, or interfaces to popular applications such as the GD Graphics Library.

Interfacing with other languages

Tcl interfaces natively with the C language. This is because it was originally written to be a framework for providing a syntactic front-end to commands written in C, and all commands in the language are implemented this way. Each command implementation function is passed an array of values that describe the arguments to the command, and is free to interpret those values as it sees fit.
Digital logic simulators often include a Tcl scripting interface for simulating Verilog, VHDL and SystemVerilog hardware languages.
Tools exist to automatically generate the necessary code to connect arbitrary C functions and the Tcl runtime, and does the reverse, allowing embedding of arbitrary C code inside a Tcl script and compiling it at runtime into a DLL.

Extension packages

The Tcl language has always allowed for extension packages, which provide additional functionality, such as a GUI, terminal-based application automation, database access, and so on. Commonly used extensions include:
; Tk: The most popular Tcl extension is the Tk toolkit, which provides a graphical user interface library for a variety of operating systems. Each GUI consists of one or more frames. Each frame has a layout manager.
; Expect: One of the other very popular Tcl extensions is Expect extension. The early close relationship of Expect with Tcl is largely responsible for the popularity of Tcl in prolific areas of use such as in Unix testing, where Expect was employed very successfully to automate telnet, ssh, and serial sessions to perform many repetitive tasks. Tcl was the only way to run Expect, so Tcl became very popular in these areas of industry.
; Tile/Ttk: Tile/Ttk is a styles and theming widget collection that can replace most of the widgets in Tk with variants that are truly platform native through calls to an operating system's API. Themes covered in this way are Windows XP, Windows Classic, Qt and Aqua. A theme can also be constructed without these calls using widget definitions supplemented with image pixmaps. Themes created this way include Classic Tk, Step, Alt/Revitalized, Plastik and Keramik. Under Tcl 8.4, this package is known as Tile, while in Tcl 8.5 it has been folded into the core distribution of Tk.
; Tix: Tix, the Tk Interface eXtension, is a set of user interface components that expand the capabilities of Tcl/Tk and Python applications. It is an open source software package maintained by volunteers in the Tix Project Group and released under a BSD-style license.
; Itcl/IncrTcl: Itcl is an object system for Tcl, and is normally named as .
; Tcllib: Tcllib is a set of scripted packages for Tcl that can be used with no compilation steps.
; Tklib: Tklib is a collection of utility modules for Tk, and a companion to Tcllib.
; tDOM: tDOM is a Tcl extension for parsing XML, based on the Expat parser
; TclTLS: TclTLS is OpenSSL extension to Tcl.
; TclUDP: The TclUDP extension provides a simple library to support User Datagram Protocol sockets in Tcl.
; Databases: Tcl Database Connectivity, part of Tcl 8.6, is a common database access interface for Tcl scripts. It currently supports drivers for accessing MySQL, ODBC, PostgreSQL and SQLite databases. More are planned for the future. Access to databases is also supported through database-specific extensions, of which there are many available.