|
|
|
|
|
|
|
|
|
|
|
Introduction
|
|
|
Tcl (originally from "Tool Command Language", but nonetheless conventionally rendered as "Tcl" rather than "TCL"; and pronounced like "tickle") is a scripting language created by John Ousterhout. Originally "born out of frustration" according to the author with programmers devising their own (poor quality) languages intended to be embedded into applications, Tcl quickly gained wide acceptance on its own and is generally thought to be easy to learn, but powerful in competent hands. It is most commonly used for rapid prototyping, scripted applications, GUIs and testing. Tcl is also used for CGI scripting. |
|
|
|
|
|
The combination of Tcl and the Tk GUI toolkit is referred to as Tcl/Tk. |
|
|
|
|
|
Some of the main features of TCL are |
|
|
|
|
|
- Everything is a command, including language structures. They are in Polish notation.
- Everything can be dynamically redefined and overridden.
- All data types can be manipulated as strings, including code.
- Extremely simple syntactic rules.
- Event-driven interface to sockets and files. Time based and user defined events are also possible.
- Flexible scope, with variable visibility restricted to lexical (static) scope by default, but uplevel and upvar allowing procs to interact with the enclosing functions' scopes.
- Simple exception handling using exception code returned by all command executions.
- Readily extensible, via C, C++, Java, and Tcl.
- Interpreted language, code can be created and modified dynamically, but still fast due to compiling into bytecode.
- Full Unicode (3.1) support, first released 1999.
- Platform independent: Win32, UNIX, Linux, Mac, etc.
|
|
|
|
|
|
TCL Basics
|
|
|
In this section we will look at the basic's of TCL. |
|
|
|
|
|
|
|
|
|
|
|
Scripts, commands, and words
|
|
|
Tcl is a string-based, interpreted command language. Its simplicity in syntax and common sensical approach to semantics makes this an easy language to learn and become proficient in. This short tutorial will concentrate on the most basic concepts to get you started as quickly as possible. |
|
|
|
|
|
A Tcl script consists of one or more commands, separated by newlines or semicolons. |
|
|
|
|
|
command arg1 arg2 arg3 ... |
|
|
|
|
|
|
|
|
Commands are the basic execution elements. A command is followed by zero or more words which are parameters or arguments, each separated by white space or tabs. |
|
|
|
|
|
ex: puts stdout "My first Tcl script." |
|
|
|
|
|
= > My first Tcl script. |
|
|
|
|
|
|
|
|
Evaluating a command
|
|
|
|
|
|
Two step process: parsing and execution. |
|
|
|
|
|
- Parsing: Tcl does not apply any meaning to the value of the words. It only performs simple string operations, e.g. variable substitutions. Tcl only makes one pass for substitutions (each character is scanned exactly once). The result of one substitution is not scanned for further substitutions.
- Execution: meaning is applied to the arguments of the command. Tcl checks to see if the command is defined, assumed to be the first word in the sequence of words, and locates a command procedure to execute.
|
|
|
|
|
|
Note: arguments are quoted by default -- if you want evaluation, you must ask for it explicitly |
|
|
For example: |
|
|
|
|
|
set a 5 |
|
|
set b a+8 |
|
|
|
|
|
The first command assigns the string 5 to variable a. The second command takes the string a+8 and stores it as the new value for b. For y to take on the value 13, you must explicitly evaluate it, as in: |
|
|
|
|
|
set a 5 |
|
|
set b [expr $a+8] |
|
|
|
|
|
Each pair of brackets invokes an additional evaluation. The one thing you must remember about Tcl is that it does just what you think it will. The evaluation model is very straightforward. There is a single command and zero or more arguments. Those arguments may in turn be commands with arguments that must be evaluated. The commands' return values become the argument to the original command to be evaluated. |
|
|
|
|
|
Variable,command and backslash substitutions
|
|
|
|
|
|
State is maintained in Tcl through the use of variables, and are both declared and instantiated with the "set" command. |
|
|
|
|
|
example: |
|
|
|
|
|
set loopCounter 0 |
|
|
|
|
|
|
|
|
Note that a separate variable declaration is not necessary. Variable values are retrieved using the dollar sign, which will be familiar to shell programmers and Perl developers, but there are slightly different rules associated with when and when not to use the dollar sign. |
|
|
|
|
|
example: |
|
|
|
|
|
if |
|
|
|
|
|
($loopCounter > 10) { |
|
|
|
|
|
do something |
|
|
|
|
|
} |
|
|
|
|
|
- $ variable substitution: variables are not declared -- they are all of type string of arbitrary length. Variable substitution can occur anywhere in a word (e.g. button .b$num).
- [ ] command substitution: everything inside the brackets is evaluated as a Tcl script.
- \ backslash substitution: escape for $, newline, quotes, etc.
|
|
|
|
|
|
Quoting and comments
|
|
|
|
|
|
- double quotes: spaces, tabs, newlines, semicolons treated as ordinary characters (normal $, command, and backslash substitution occur as usual).
- curly braces: all special characters lose their meaning (no substitutions take place). Braces defer evaluation.
- comments: if first non-blank character is a '#', everything up to the newline is a comment. If '#' occurs anywhere else, it is treated as an ordinary character.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|