from Wikipedia, the free encyclopedia

Script language with bytecode interpreter
Basic data
Paradigms : imperative , functional , object-oriented
Publishing year: 1988
Designer: John Ousterhout
Developer: John Ousterhout
Current  version 8.6.9   (November 16, 2018)
Typing : weak , dynamic
Influenced by: Lisp , Unix Shell , C
Operating system : Windows , Linux , macOS , Mac OS Classic , Solaris , other Unix variants and clones
License : BSD
Tcl Developer Site

Tcl (debate English tickle or as shortcut for Tool command language ) is an open-source - scripting language .

Tcl was originally developed in 1988 by John Ousterhout at the University of California, Berkeley, as a macro language for an experimental CAD system. The concept of using the Tcl interpreter as a library in z. B. to integrate a C program, which is still possible today.

The slogans of Tcl are: "radically simple" , which relates in particular to the syntax of the language and "everything is a string", "everything is text", which relates to the handling of commands and data in Tcl refers.

The common combination of Tcl and the GUI toolkit Tk is called Tcl / Tk .

Basic concepts

Simple syntax and grammar

The Tcl syntax follows the Polish notation . It dispenses with reserved words , but assigns a fixed meaning to some characters:

  • the curly braces to define blocks,
  • the square brackets for evaluating expressions,
  • the quotation marks to separate strings,
  • the backslash ,
  • the double cross for comments,
  • the semicolon to separate commands and
  • the end of the line.

All other components of the language can be redefined. There is no difference between built-in functions and functions added by programs or Tcl libraries.

Data types

Tcl is a (outwardly) typeless language. Each variable has a character string as a value. For this purpose, an internal representation can e.g. B. an integer , floating point number , list (data structure) or dict . The use of an undefined variable leads to an error - in contrast to programming with the Unix command line interpreter ( shell ) or awk . Constructs such as associative arrays ( hash table ), lists and dicts are often used in Tcl. Dicts are comparable to JSON , but apart from the curly braces without additional special characters such as quotation marks and colons. There are also objects with classes, multiple inheritance and mixins . The latter, like the control elements of the graphical user interface Tk, are commands within Tcl.


Tcl knows very powerful commands for processing (also long) character strings, as well as file processing , TCP / IP network communication and graphic programming via Tk and is completely platform-independent in all of this. Tcl has built in a mechanism to work with regular expressions , although more complex expressions than grep are supported, similar to Perl's .


To integrate external libraries, Tcl has its own package system, which can also be reloaded automatically if necessary. It is also possible to extend Tcl programs with libraries that are written in C or another compiled language; A standardized interface exists for this in the form of TclStubs. In addition, with the help of the CriTcl extension, time-critical program parts can be noted in C source code within the Tcl source code. These are automatically compiled and integrated.

Self-modifying code

Tcl programs can very easily modify themselves at runtime. Since it is easily possible to implement your own control structures in pure Tcl, it is possible to implement various programming paradigms directly in Tcl, for example functional or object-oriented programming .

In addition, the self-modifiability allows code to be read and executed from configuration files or over the network. To enable this in a secure form, Tcl provides any number of sandboxes in the form of specially started interpreters with limited functionality. These child interpreters can each be expanded with their own functions, which communicate with their parent interpreter via defined interfaces .

Object orientation

Tcl contains the current expansion in the core (8.6 version) TclOO with single and multiple inheritance and mixins so completely object-oriented applications can be written - but not necessarily. Classes contain constructors and destructors as well as methods . In contrast to other programming languages, classes and objects are implemented as commands and must be explicitly destroyed using "destroy", which can be automated by monitoring variables using "trace" if the variable goes out of scope.

Since there are no pointers , other objects are referred to using the object name instead.


Tcl also implements concurrency if required . Each thread has its own interpreter and therefore its own variables. A thread can instruct another thread to execute commands. Threads have a parent-child relationship. The synchronization takes place via mutexes or via "join". An alternative implementation of concurrency via coroutines is also available from Version 8.6.

Bytecode interpreter

Tcl routines are translated into bytecode by the interpreter the first time they are executed . When a routine is executed the second time, the bytecode is available and the program part runs faster. There are also extensions that translate the entire source code into bytecode when the program is loaded.

GUI interfaces

Tcl is also known for the toolkit Tk , with which platform-independent graphical user interfaces can be easily programmed. The graphic toolbox "Tk" is available for a variety of operating systems with the usual appearance ("native look and feel") for the respective system. This programming interface is also offered for many other programming languages, such as B. Common Lisp , Perl , PHP , Ruby , Python or R . In addition to the standard interface to the Tk Toolkit, there are also interfaces to the FLTK and GTK + toolkits .

Other properties and special features

  • "Everything is a string" principle: Even if a distinction is made internally - mainly for performance reasons - between strings, integers, floating point numbers, lists and dicts (the translation of "dictionaries" is inappropriate), all of these can be used at any time can be addressed as a character string or character strings with corresponding content can be addressed as the specialized types. However, the implicit conversion then costs computing power, which is why care should be taken to avoid this "shimmering".
  • Event- controlled interfaces to sockets and files so that you can work with network connections and the GUI even without threads. Time- and user-defined events are also possible.
  • The variable visibility area is limited to local variables, but can be specifically extended to the visibility area of ​​the calling function with uplevel and upvar .
  • Simple exception handling through exception return values ​​of all commands.
  • Easily expandable in C , C ++ , Java and Tcl.


In principle, Tcl has a very simple structure and distinguishes itself from languages ​​such as Perl , APL and C through the absolutely consistent use of a uniform syntax . Anyone who is familiar with command line interpreters ( Shell , MS-DOS ) also knows the basic structure of Tcl commands. A Tcl script consists of several commands. A command consists of a command word followed by arguments ( parameters ). A command is delimited by an end of line or a semicolon .

Kommandowort param1 param2 … paramN

In contrast to simple command line interpreters, commands can be nested in Tcl. Instead of an argument in a command, another command can be specified in square brackets. The subcommands are executed first. Your result is then used as an argument in the higher-level command. The mechanism corresponds to that of the backquotes in the Unix shell .

Kommandowort [Unterkommando param …] …

Constructs such as if and while or assignments are also commands. The commands follow the Polish notation , as with Lisp , and are also processed as a list .

Areas of application

Tcl is already pre-installed in most Unix installations or can be installed later using the package management , even with Apple macOS ; but not with Microsoft Windows . There are also different installation packages for other operating systems including Windows. Tcl is platform-independent and behaves the same on all systems for which it is available. Usually a Tcl program (script) is started via the Tcl shell tclsh for programs with non-graphical input / output or the Tcl windowing shell wish for programs with a graphical user interface.

Tcl is on the command line , as an embedded language, as CGI (otherwise often as -Language Perl ), as a module in the Apache - Web server (otherwise often as PHP ), and as the language for procedures in the database PostgreSQL used. It can be easily expanded to C via a simple interface .

Sample programs

A "Hello World!" Program

puts "Hello World!"

Hello World!

The puts command expects a character string as input and outputs it directly, followed by a line break. Here is the same output using the command to set a variable value:

set hw "Hello World!"
puts $hw

Hello World!

Average of a list of numbers

proc mean data {
    expr ([join $data +]) / double([llength $data])

This defines a new command mean , which can be called as follows

mean {5 4.2 1.2 6.7 9 1 0}

So data is a list of numbers. The join command forms a character string of the form 5 + 4.2 + 1.2 + 6.7 + 9 + 1 + 0 from its first parameter $ data (content of data ) using the second parameter + . This is now inserted in the place where the join command enclosed in square brackets was previously . The llength command returns the length of a list. The square brackets work the same way here. The double () function ensures that the numbers are not divided as an integer with a remainder, but as floating point numbers with decimal places (this is usually intended for mean values).

For the example it results:

expr (5+4.2+1.2+6.7+9+1+0)/double(7)

The expr command now calculates the mathematical expression.

The example shows how easily character string processing and calculations can be mixed in Tcl in order to formulate algorithms easily.

Graphic application programming with Tk

Tcl makes the development of graphical user interfaces very easy: The following mini-program creates a button in the window which, when clicked, exits the application.

package require Tk
pack [button .b -text "Goodbye World" -command exit]

In addition to the classic "Tk widget set" (these are the operating elements of the graphical user interface), which, depending on the platform , simulates the appearance of Motif , Microsoft Windows, or Mac OS Classic , the Ttk widget set (themeable Tk) fixed to Tk. A theme ("theme") can be selected from a theme library or created yourself.

package require Tk
ttk::setTheme clam
pack [ttk::button .b -text "Goodbye World" -command exit]

Database connection with Tcl

Database operations are also very easy with Tcl, as the following example shows:

# SQLite3 einbinden
package require sqlite3
# Datenbank eröffnen
sqlite3 meinedatenbank ./meinedatenbank.sqlite
# Variable zum Referenzieren
set var 3
# Tabelle erzeugen, Fehlermeldung ignorieren
meinedatenbank eval {CREATE TABLE tabelle1 (id int, spalteA char(20))}
# Daten in Tabelle schreiben
meinedatenbank eval {INSERT INTO tabelle1 (id, spalteA) VALUES (1, 'foo'), (2, 'bar'), (3, 'ßülz')}
# Daten abfragen
meinedatenbank eval {SELECT * FROM tabelle1 WHERE id = :var} ergebnis {
   puts "id = $ergebnis(id), spalteA = $ergebnis(spalteA)"
# Alle Daten löschen
meinedatenbank eval {DELETE FROM tabelle1}
# Tabelle löschen
meinedatenbank eval {DROP TABLE tabelle1}
# Datenbank schließen
meinedatenbank close

Variable references are not expanded, but transferred to the database "engine" so that no security gap can arise through SQL injection .

This direct use of the SQLite3 interface is now considered obsolete because there is the database-independent interface TDBC , the name of which is based on ODBC and JDBC. The TDBC scope of delivery includes the drivers for SQLite3 , MySQL , ODBC (similar to the JDBC-ODBC-Bridge ) and PostgreSQL .


Tcl can be used as a procedural as well as a functional programming language , since names of functions can also be arguments of functions. With extensions like stooop , Snit , Incr Tcl and Incr Tk as well as XOTcl , Tcl is also object-oriented - up to multiple inheritance . From version 8.6 TclOO is contained in the kernel, Incr Tcl is now based on TclOO .

  • XOTcl is one of several object-oriented extensions to Tcl written in C. Similar to Common Lisp Object System , it supports metaclasses , which define properties of classes, and is a fully dynamic object-oriented language. This means that, on the one hand, definitions of classes and methods can be changed dynamically (at runtime) and, on the other hand, the relationships between objects and classes as well as between classes can be changed at any time. This means that, for example, an object can change its class (e.g. an object of the “car” class becomes an object of the “wreck” class if it hits a tree) or the class hierarchy can be changed dynamically. XOTcl offers the classic object-oriented concepts and mixin classes, which is here between per-object-mixins and per-class mixins distinguished. Using mixin classes, orthogonal behavior can be implemented separately.
  • Incr Tcl and Incr Tk are packages of object-oriented extensions for Tcl and Tk . These enable object-oriented programming with Tcl. The names are based on those of C ++. The increment procedure incr corresponds to the ++ operator in C.
  • Snit ("Snit's Not Incr Tcl") is a package for object-oriented programming with Tcl, which does not need to be compiled as a pure Tcl package. In contrast to Incr Tcl (inheritance) it uses the principle of delegation for the realization of the object orientation.
  • Tile , additional package in Tcl / Tk 8.4, and Ttk , standard package since Tcl / Tk 8.5, are libraries for graphic user elements that enable a native appearance on the different operating systems. This enables Tcl / Tk applications to have a modern look on Unix operating systems. When using the standard widgets of the Tk library, they use the outdated Motif elements.
  • Tcl3D is a package that extends Tcl with functionality for 3D graphics programming. This z. B. Create viewers for .dxf files.


  • The Tcl Core Team provides the reference implementation. The ActiveTcl distribution is based on this .
  • Jim tcl is a minimalist implementation with a focus on embedded systems .

See also

Web links

Wikibooks: Tcl programming  - learning and teaching materials (English)

Individual evidence

  1. Latest release: Tcl / Tk 8.6.9 (Nov 16 2018) . (English).
  2. TDBC. Website of the provider
  3. (website for reference implementation)
  4. website