Developer Tcl Core Team | ||
![]() | ||
Paradigm First appeared 1988; 29 years ago (1988) Stable release 8.6.6 / July 27, 2016; 7 months ago (2016-07-27) Typing discipline dynamic typing, everything can be treated as a string |
Tcl (pronounced "tickle" or tee cee ell, /ˈtiː siː ɛl/) 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.
Contents
- History
- Features
- Safe Tcl
- Syntax and fundamental semantics
- Basic commands
- Advanced commands
- Uplevel
- Upvar
- Object oriented
- Interfacing with other languages
- Module Files
- Extension packages
- References
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 (GUI) 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. John 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".
Tcl conferences and workshops are held in both the United States and Europe.
Features
Tcl's features include
uplevel
and upvar
allowing procs to interact with the enclosing functions' scopes.Safe-Tcl
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 are covered by the twelve rules of the dodecalogue (alternative wording).
A Tcl script consists of several command invocations. A command invocation is a list of words separated by whitespace and terminated by a newline or semicolon.
word0 word1 word2 ... wordNThe first word is the name of a command, which is not built into the language, but which is in the library. The following words are arguments. So we have:
commandName argument1 argument2 ... argumentNAn example, using the puts command to display a string on the host console, is:
This sends the string "Hello, World!" to the 'stdout' device, with an appended newline character.
Variables and the results of other commands can be substituted inside strings too, such as in this example where we use set and expr (no assignment operator =) to store a calculation result in a variable, and puts (short for "put string") to print the result together with some explanatory text:
There is one basic construct (the command) and a set of simple substitution rules.
Formally, words are either written as-is; with double-quotes around them (allowing whitespace characters to be embedded); or with curly-brace characters around them, which suppresses all substitutions inside (except for backslash-newline elimination). In bare and double-quoted words, three types of substitution occur (once, in a single left-to-right scan through the word):
expr
command does.From Tcl 8.5 onwards, any word may be prefixed by “{*}” to cause that word to be split apart into its constituent sub-words for the purposes of building the command invocation (similar to the “,@” sequence of Lisp's quasiquote feature).
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 (e.g. the presence of spaces in filenames does not cause difficulties).
The single equality sign (=) for example is not used at all, and the double equality sign (==) is the test for equality, and even then only in expression contexts such as the expr
command or the first argument to if
. (Both of those commands are just part of the standard library; they have no particularly special place in the library and can be replaced, if so desired.)
The majority of Tcl commands, especially in the standard library, are variadic, and the proc
(the constructor for scripted command procedures) 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 (subject to syntactic constraints) 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:
set
writes a new value to a variable (creates a variable if did not exist). If used only with one argument, it returns the value of the given variable (it must exist in this case).proc
defines a new command, which's execution results in executing a given Tcl script, written as a set of commands. return
can be used to immediately return control to the caller.The usual execution control commands are:
if
executes given script body (second argument), if the condition (first argument) is satisfied. It can be followed by additional arguments starting from elseif
with the alternative condition and body, or else
with the complementary block.while
repeats executing given script body, as long as the condition (first argument) remains satisfiedforeach
executes given body where the control variable is assigned list elements one by one.for
shortcut for initializing the control variable, condition (as in while
) and the additional "next iteration" statement (command executed after executing the body)Those above looping commands can be additionally controlled by the following commands:
break
interrupts the body execution and returns from the looping commandcontinue
interrupts the body execution, but the control is still given back to the looping command. For while
it means to loop again, for for
and foreach
, pick up the next iteration.return
interrupts the execution of the current body no matter how deep inside a procedure, until reaching the procedure boundary, and returns given value to the caller.Advanced commands
expr
passes the argument to a separate expression interpreter and returns the evaluated value. Note that the same interpreter is used also for "conditional" expression for if
and looping commands.list
creates a list comprising all the arguments, or an empty string if no argument is specified. The lindex
command may be used on the result to re-extract the original arguments.array
manipulates array variables.dict
manipulates dictionary (since 8.5), which are lists with an even number of elements where every two elements are interpreted as a key/value pair.regexp
matches a regular expression against a string.uplevel
is a command that allows a command script to be executed in a scope other than the current innermost scope on the stack.upvar
creates a link to variable in a different stack frame.namespace
lets you create, access, and destroy separate contexts for commands and variables.apply
- apply an anonymous function (since 8.6).coroutine
, yield
, yieldto
create and produce values from coroutines (since 8.6).try
lets you trap and process errors and exceptions.catch
lets you trap exceptional returns.zlib
provides access to the compression and check-summing facilities of the Zlib library (since 8.6).Uplevel
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 (like for, if or while) and still have the ability to manipulate local variables. For example, the following Tcl script is a reimplementation of the for command (omitting exception handling):
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:
Object-oriented
Tcl 8.6 added a built-in dynamic object system, TclOO, in 2012. It includes features such as:
Tcl did not have object oriented (OO) syntax until 2012, so various extension packages emerged to enable object-oriented programming. They are wide spread in existing Tcl source code. Popular such 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.
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 (including things that might otherwise be keywords, such as if
or while
) are implemented this way. Each command implementation function is passed an array of values that describe the (already substituted) 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 (e.g. SWIG, Ffidl) to automatically generate the necessary code to connect arbitrary C functions and the Tcl runtime, and Critcl does the reverse, allowing embedding of arbitrary C code inside a Tcl script and compiling it at runtime into a DLL.
Module Files
Environment Modules are written in the Tcl (Tool Command Language) and are interpreted by the modulecmd program via the module user interface.
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: