Hosted by |
|
Tcl is a very simple programming language. If you have programmed before, you can learn enough to write interesting Tcl programs within a few hours. This page provides a quick overview of the main features of Tcl. After reading this you'll probably be able to start writing simple Tcl scripts on your own; however, we recommend that you consult one of the many available Tcl books for more complete information. Basic syntaxTcl scripts are made up of commands separated by newlines or semicolons. Commands all have the same basic form illustrated by the following example:
This command computes the sum of 20 and 10 and returns the result, 30.
You can try out this example and all the others in this page by
typing them to a Tcl application such as
Each Tcl command consists of one or more words separated
by spaces. In this example there are four words:
However, for most commands the word structure is important, with each word used for a distinct purpose. All Tcl commands return results. If a command has no meaningful result then it returns an empty string as its result. Variables
Tcl allows you to store values in variables and use the values later
in commands. The
The command returns the new value of the variable. You can read the
value of a variable by invoking
You don't need to declare variables in Tcl: a variable is created automatically the first time it is set. Tcl variables don't have types: any variable can hold any value. To use the value of a variable in a command, use variable substitution as in the following example:
When a
Command substitutionYou can also use the result of one command in an argument to another command. This is called command substitution:
When a Quotes and bracesDouble-quotes allow you to specify words that contain spaces. For example, consider the following script:
After these three commands are evaluated variable Curly braces provide another way of grouping information into words. They are different from quotes in that no substitutions are performed on the text between the curly braces:
This command sets variable Control structures
Tcl provides a complete set of control structures including commands
for conditional execution, looping, and procedures. Tcl control
structures are just commands that take Tcl scripts as arguments.
The example below creates a Tcl procedure called
This script consists of a single command,
When
The body of the Where do commands come from?As you have seen, all of the interesting features in Tcl are represented by commands. Statements are commands, expressions are evaluated by executing commands, control structures are commands, and procedures are commands. Tcl commands are created in three ways. One group of commands is provided by the Tcl interpreter itself. These commands are called builtin commands. They include all of the commands you have seen so far and many more (see below). The builtin commands are present in all Tcl applications. The second group of commands is created using the Tcl extension mechanism. Tcl provides APIs that allow you to create a new command by writing a command procedure in C or C++ that implements the command. You then register the command procedure with the Tcl interpreter by telling Tcl the name of the command that the procedure implements. In the future, whenever that particular name is used for a Tcl command, Tcl will call your command procedure to execute the command. The builtin commands are also implemented using this same extension mechanism; their command procedures are simply part of the Tcl library. When Tcl is used inside an application, the application incorporates its key features into Tcl using the extension mechanism. Thus the set of available Tcl commands varies from application to application. There are also numerous extension packages that can be incorporated into any Tcl application. One of the best known extensions is Tk, which provides powerful facilities for building graphical user interfaces. Other extensions provide object-oriented programming, database access, more graphical capabilities, and a variety of other features. One of Tcl's greatest advantages for building integration applications is the ease with which it can be extended to incorporate new features or communicate with other resources.
The third group of commands consists of procedures created with
the Other featuresTcl contains many other commands besides the ones used in the preceding examples. Here is a sampler of some of the features provided by the builtin Tcl commands:
ExamplesTutorialHave some time to delve a bit further into the language? The Tcl Tutorial is a great way to learn more about Tcl's various commands and language features. |