| 2.2 Variables in Tcl |
|
|
In Tcl, variable names can be almost any string of characters. The one
restriction, explained below in
Regional Variables, is that a variable name cannot
contain ::. However, taking full advantage of Tcl's free form variable
names requires finesse and does not make for readable scripts. Accordingly, I
suggest you begin your variables names with a letter and continue them with
alphameric characters and the underscore character "_." (By
Remark
Other than its name, the other important aspect of a variable is its value. Variable values can also be arbitrary strings. In fact, they are always either strings or one-dimensional arrays of strings the only data types in Tcl are strings and one-dimensional arrays. You never declare a Tcl variable's type. The interpreter can infer that type from syntactical hints. You must, however, assign values to variables before you use them there are no default initial values. When numbers are the values of variables, they have their ASCII form and not one of the internal forms your computer uses for calculations. Unlike most programming languages, in Tcl, when you write the name of a variable, you mean the name not the value. That is why
X + 1would be an error if you meant "add one to X." The reason is that "X" can only be the name of a variable, not its value. To represent the value of a variable, put a dollar sign in front of it, for example, $X.
Before each line of Tcl code is executed, something called
Two caveats:
You already know that constructs like while which control the flow of
execution are implemented as procedures. You might guess that variable
assignment is too. You would be correct. The procedure is called
% set X 2 2 % set Y $X 2As with many examples in this book, this one shows an interactive session. Lines beginning with the % prompt are command lines. Other lines are generated by Tcl in one of two ways: they show the return string from the command on the previous line or they are the result of a puts command. In this example, the first statement assigns the value 2 to the variable named X and returns the value. The second statement assigns the value of the variable named X to the variable named Y and returns the value. Of course, when both statements have executed, the value of both variables is a string containing the numeric character 2. By the way, Tcl handles return strings differently when executed interactively than when executed directly. Interactive execution works as shown with this example: return strings are printed in the command window. With direct execution, return strings are normally ignored. The exception is explained below in Command Substitution. Remark
Even though the set procedure returns the value that it assigns, this return value is ignored except when a command is executed in a context that requires a return value. The set procedure can also be used with just one argument. In that case, the argument is viewed as a variable name and the value of that variable is returned. Thus, set X is similar to $X but they are appropriate in somewhat different contexts. |
Author's Home Page |
|
Order from Amazon. |