PLE notes -- Tcl

Designed to be "universal," Tcl is the paradigmatic interpreted scripting language. Beside the usual features of interpreted scripting languages, Tcl made extensibility a key design issue. This was ensured by giving the language a minimal core on which all advanced constructs had to be implemented. The theory is that if Tcl can implement its own "proc" statement then it can implement anything else you want. While successful, this approach was perhaps extreme.


Tcl has no grammar, data types, or control structures. The only hard-wired features deal with manipulating syntax: quoting and substitution. Tcl commands are similar to csh commands, except they call a C function instead of an executable file. After Tcl applies its quoting and substitution rules to a command, the individual words are passed, as strings, to the C function named by the first word of the command. These functions are created by registering them with the interpreter (as opposed to csh functions, which are found by roaming around the file system). Just as sh evaluates conditions via the external test command, Tcl evaluates expressions, conditionals, and loops with external C functions. (Loops are just commands that take scripts as arguments.)


The result is a very extensible language. New control mechanisms and even naming systems (e.g. object orientation) can be implemented as extensions. The ability to bind a command for any C function allows Tcl to drive graphics toolkits (Tk), databases, and hardware devices.


However, Tcl's reductionist approach to extensibility has drawbacks. No matter how many extensions are added, Tcl's level of abstraction is still syntactic. This means the user must always think in terms of quoting and substitution, not the abstractions suited to their task. The kinds of logical errors a Tcl programmer can make, e.g. forgetting to quote an expression, are quite different from those a Python programmer can make. The semantics of quoting require the user to think about the meaning the interpreter assigns to their program, instead of the meaning they, as humans, assign to the program. For example, when a quoted expression is evaluated a second time, it can produce different results. A tragic consequence is that even though Tcl is interpreted, dynamic generation of Tcl scripts is severely complicated by the quoting rules. (The problems are similar to defining aliases in csh.)

PLE Home page
Last modified: Tue Jan 30 22:37:11 EST 1996