Tcl

Tcl (debate engl. Tickle or as an abbreviation 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. From this period dates the concept to include the Tcl interpreter as a library, for example, in a C program, which is possible even today.

Are the mottoes of Tcl: " radically simple", that is " radically simple " what in particular refers to the syntax of the language and " everything is a string", "everything is text", which is reflected in the handling of commands and data Tcl relates.

The common combination of Tcl and the Tk toolkit is referred to as Tcl / Tk.

  • 4.1 A "Hello World! " Program
  • 4.2 average of a list of numbers
  • 4.3 Graphical application programming with Tk
  • 4.4 Database connectivity with Tcl

Basic Concepts

Simple syntax and grammar

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

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

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

Data types

Tcl is a ( toward the outside ) typeless language. Each variable has a string value. This can occur, for example, an internal representation of an integer, floating point number or list. The use of an undefined variable causes an error - in contrast to programming with the Unix command line interpreter ( shell) or awk. Constructs such as associative array ( hash table ) and lists are applied in Tcl often.

Strings

Tcl knows very powerful commands for editing (also long ) strings - at least equal to those of Perl and Python - as file editing, TCP / IP network communication and Tk graphical programming and is completely platform-independent in all of this. Tcl has built a mechanism to work with regular expressions can, with even more complex expressions than are supported by grep, comparable to those of Perl.

Extensibility

To incorporate external libraries, Tcl has its own package system that can automatically reload also when needed. Furthermore, it is possible to Tcl programs to expand libraries that are written in C or other compiled language; this is shown by a standardized interface in the form of TclStubs. In addition, using the CriTcl extension time-critical parts of the program in C source code can be recorded within the Tcl source code. These are automatically compiled and included.

Self-modifying code

Tcl programs can very easily be modified at runtime itself. Since it is readily possible to implement your own control structures in pure Tcl, it's possible to implement different programming paradigms in Tcl directly, for example functional or object-oriented programming.

Also can be read and executed by the configuration files from Selbstmodifizierbarkeit code or via the network. To make this possible in a secure form, Tcl provides an arbitrary number of sandboxes in the form of specially prepared starting interpreter with limited functionality. This child interpreter can be extended each with its own features that communicate via defined interfaces with their mother interpreter.

Concurrency

Tcl implemented by request concurrency. Each thread has its own interpreter. The synchronization is done via an extension. An alternative implementation of concurrency on coroutines available starting in Version 8.6 available.

Bytecode compiler

Tcl programs are translated by the interpreter in each case prior to the first run in bytecode. In the second performing a routine then the bytecode is already available and the process happens faster. There are also extensions that make available the bytecode already at load time of the program.

GUI interfaces

Tcl is also known by the Tk toolkit, which can be platform - independent graphical user interfaces easy to program. The graphical toolbox "TC " stands for a variety of operating systems with the usual for the particular system appearance ( "native look and feel" ) are available. This programming is also available for many other programming languages ​​, such as Common Lisp, Perl, PHP, Ruby, Python or R. In addition to the standard interface to the Tk Toolkit, among other things exist interfaces to the toolkit FLTK and GTK.

Further characteristics and peculiarities

  • Event-driven interfaces to sockets and files. Time and user - defined events are also possible.
  • Variable Scope limited to local variables, but can be expanded with uplevel and upvar specifically to the scope of the calling function.
  • Simple exception handling by exception return values ​​of all commands.
  • Simple Extensibility in C, C , Java, and Tcl.

Syntax

Tcl is very simple in principle and distinguishes itself from languages ​​such as Perl, APL and C through absolutely consistent use of a uniform syntax from. Anyone familiar with command-line interpreter (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 a newline or semicolon. Over simple command-line interpreter, but has Tcl via the ability to nest into each other commands. Instead of an argument in a command, another command can be given in square brackets. The sub- commands are executed first. Your result is then used in each case as an argument in the parent command. The mechanism corresponds to the backquotes in the Unix shell.

Also constructs such as if and while are commands or assignments. The commands follow the Polish notation, such as Lisp, and are also processed as a list. The command word is at the beginning, followed by the parameters:

Param1 param2 ... command token paramN areas of application

Tcl is pre-installed in most Unix installations. There are also different installation packages for other operating systems. Tcl is platform-independent and acts on all systems for which it is present, the same. Usually, a Tcl program is started ( script) on the Tcl shell tclsh for programs with non- graphical Ein-/Ausgabe or the Tcl windowing shell wish for GUI programs.

Tcl is (otherwise often as PHP) used on the command line, as an embedded language as a CGI language ( as is often the Perl ) as a module in the Apache web server and as the language for procedures in the database PostgreSQL. It is easily extensible through a simple interface to C.

Example Programs

A "Hello World! " Program

Puts " Hello World!" Hello World!

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

Set hw "Hello World! " puts $ hw Hello World!

Mean 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 } data is thus a list of numbers. The join command formed from his first parameter $ data ( content data) using the second parameter a string of the form 5 4.2 1.2 6.7 9 1 0. This string is then inserted into the position where the previous state of the area enclosed by square brackets join command. The llength command returns the length of a list. The brackets work here as well. The function double ( ) causes the numbers are divided not as an integer with the rest, but as floating point numbers with decimal places ( that is with mean values ​​typically intended).

It follows for the example:

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 simple Tcl string processing and calculations can be mixed to formulate algorithms concise.

Graphical application programming with Tk

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

Package require Tk pack [ button. b -text " Goodbye World" -command exit] In addition to the classic Tk widget set that simulates the look of Motif, MS- Windows, or MacOS Classic, depending on the platform heard since version 8.5 and the widget set tile firmly Tk context, a theme from a theme library are selected or created yourself.

Package require Tk ttk :: setTheme clam pack [ ttk :: button. b -text " Goodbye World" -command exit] Database connectivity with Tcl

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

Package require sqlite3 sqlite3 my database. / meinedatenbank.sqlite set var "foo" myDatabase eval { SELECT * FROM table1 WHERE spalteA = $ var } { result-     puts $ result ( spalteB ) } myDatabase close This variable references are not expanded, but passed to the database engine, so no vulnerability SQL injection can occur.

Extensions

TCL can be used as a procedural programming language as well as a functional because the names of functions may also be arguments of functions. About extensions such stooop, Snit, Incr Tcl and Tk and Incr Tcl XOTcl is object-oriented - right down to multiple inheritance.

  • XOTcl is an object- oriented extensions of several of Tcl, which was written in C. It supports similar to Common Lisp Object System metaclasses that define the properties of classes and is a fully dynamic object-oriented language. This means that on the one hand definitions of classes and methods dynamically (at run time) can be changed, and that on the other hand, the relationships between objects and classes and between classes are mutually changed at any time. This means that, for example, an object can change its class (eg, an object of class " car " is the object of class " wreck " when it goes against a tree ), or the class hierarchy can be changed dynamically. XOTcl features in addition to the classic object-oriented concepts via so-called mixin classes by here a distinction between per- object mixins - and per- class mixins. By mixin class, it is possible to implement orthogonal separated behavior.
  • Incr Tcl and incr Tk packages object-oriented extensions to Tcl and Tk These allow object-oriented programming with Tcl. The names are inspired by the C . The increment incr procedure corresponds to the operator in C.
  • Snit ( " Snit 's Not Incr Tcl " ) is a package for object-oriented programming with Tcl, which must not 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, additive package in Tcl / Tk 8.4, and Ttk, standard package for Tcl / Tk 8.5, are libraries for graphical Benutzerlemente allow a native look on the different operating systems. Thus, on Unix operating systems, Tcl / Tk applications a modern appearance are possible. When using the standard widgets of Tk library use this obsolete Motif elements.
  • Tcl3D is a package that extends Tcl functionality for 3D graphics programming. Thus, eg viewer. Dxf files built.
411273
de