| 2.5 Curly Brackets |
|
|
Here is a while loop for copying a file line by line. The condition for continuing the loop is shown partially in pseudocode.
while {-1 != the value obtained by executing gets $InFile Line} {
puts $OutFile $Line
}
Two questions come immediately to mind:
When curly brackets surround a command argument, they cause the Tcl interpreter to consider everything between the curly brackets as a sealed string that does not need processing. In particular, it needs no substitutions and no consideration of end-of-line symbols. More precisely,
This means that the while statement shown above looks like
while {CONDITION} {BODY}
and there is no variable substitution within the curly brackets. What you
see for CONDITION and BODY is what the while procedure sees. At
the beginning of each iteration, when while is evaluating the
CONDITION argument, it performs substitutions as a part of that
evaluation. During each iteration, while performs substitutions to the
lines in BODY. These substitutions are normal Tcl substitutions. They
have been suppressed by the curly brackets and are performed by while
as it goes about its business.
Returning to the file copying loop, the while procedure will execute this line over and over:
puts $OutFile $LineEach execution of the line begins with Tcl's normal substitutions the same substitutions that were suppressed by the use of the curly brackets around the second argument. The curly brackets ensure that it is the strings $Outfile and $Line that are available when the line is executed not the values these strings represent. By delaying the substitution of these variables, Tcl allows the values to change during execution of the while loop. Here are some examples that make additional points about curly brackets. As you read these examples, be aware that in them puts has just one argument. This fact may not be apparent as the examples appear to you.
|
Author's Home Page |
|
Order from Amazon. |