18-12-2012, 04:35 PM
Tcl Fundamentals
1Tcl Fundamentals.pdf (Size: 213.42 KB / Downloads: 34)
Tcl is a string-based command language.
The language has only a few fundamental constructs and relatively little
syntax, which makes it easy to learn. The Tcl syntax is meant to be simple. Tcl is
designed to be a glue that assembles software building blocks into applications.
A simpler glue makes the job easier. In addition, Tcl is interpreted when the
application runs. The interpreter makes it easy to build and refine your application
in an interactive manner. A great way to learn Tcl is to try out commands
interactively. If you are not sure how to run Tcl on your system, see Chapter 2 for
instructions for starting Tcl on UNIX, Windows, and Macintosh systems.
This chapter takes you through the basics of the Tcl language syntax. Even
if you are an expert programmer, it is worth taking the time to read these few
pages to make sure you understand the fundamentals of Tcl. The basic mechanisms
are all related to strings and string substitutions, so it is fairly easy to
visualize what is going on in the interpreter. The model is a little different from
some other programming languages with which you may already be familiar, so
it is worth making sure you understand the basic concepts.
Tcl Commands
Tcl stands for Tool Command Language. A command does something for you, like
output a string, compute a math expression, or display a widget on the screen.
Tcl casts everything into the mold of a command, even programming constructs
4 Tcl Fundamentals Chap. 1
like variable assignment and procedure definition. Tcl adds a tiny amount of
syntax needed to properly invoke commands, and then it leaves all the hard work
up to the command implementation.
Variables
The set command is used to assign a value to a variable. It takes two arguments:
The first is the name of the variable, and the second is the value. Variable names
can be any length, and case is significant. In fact, you can use any character in a
variable name.
Command Substitution
The second form of substitution is command substitution. A nested command is
delimited by square brackets, [ ]. The Tcl interpreter takes everything between
the brackets and evaluates it as a command. It rewrites the outer command by
replacing the square brackets and everything between them with the result of
the nested command. This is similar to the use of backquotes in other shells,
except that it has the additional advantage of supporting arbitrary nesting of
commands.
Backslash Substitution
The final type of substitution done by the Tcl interpreter is backslash substitution.
This is used to quote characters that have special meaning to the interpreter.
For example, you can specify a literal dollar sign, brace, or bracket by
quoting it with a backslash. As a rule, however, if you find yourself using lots of
backslashes, there is probably a simpler way to achieve the effect you are striving
for. In particular, the list command described on page 61 will do quoting for
you automatically.
Grouping with Braces and Double Quotes
Double quotes and curly braces are used to group words together into one argument.
The difference between double quotes and curly braces is that quotes allow
substitutions to occur in the group, while curly braces prevent substitutions.
This rule applies to command, variable, and backslash substitutions.
Square Brackets Do Not Group
The square bracket syntax used for command substitution does not provide
grouping. Instead, a nested command is considered part of the current group. In
the command below, the double quotes group the last argument, and the nested
command is just part of that group.
Grouping before Substitution
The Tcl parser makes a single pass through a command as it makes grouping
decisions and performs string substitutions. Grouping decisions are made
before substitutions are performed, which is an important property of Tcl. This
means that the values being substituted do not affect grouping because the
grouping decisions have already been made.
The following example demonstrates how nested command substitution
affects grouping. A nested command is treated as an unbroken sequence of characters,
regardless of its internal structure. It is included with the surrounding
group of characters when collecting arguments for the main command.