Navigation Logo 2.5  Curly Brackets Navigation Logo

 

 

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:

  1. How can this work when it appears on three lines instead of one?

  2. How can this work if the variables are substituted before the while procedure is executed?
The answers to both questions lie in the way curly brackets are processed.

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,

  • the interpreter forms everything inside these brackets into a string

  • while forming the string, it does not do any substitutions or pay attention to blanks or new lines. (An exception appears below in the More about Substitution.)

  • when done, it throws away the curly brackets and passes the string as an argument to the procedure

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 $Line
Each 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.

  • Curly brackets may be nested. For example,
    % puts {a{x}b}
    a{x}b
    

  • An argument is considered to be surrounded by curly brackets if, and only if, it begins with a curly bracket. In the next example, the curly brackets do not surround the argument. Therefore, they do not affect the substitution of the variable named X.
    % puts 1{$X}1
    1{$X}1
    
    The next example shows an error.
    % puts {X}1
    extra characters after close-brace
    
    The error message is puts' return string. The error is that the argument begins with a curly bracket but is not surrounded by curly brackets.

Exercise 2.5a

Write a Tcl command that assigns the string "Hello World" (without the quotes) to the variable named X.

Solution

Exercise 2.5b

What are the values of X, Y, and Z after these statements have executed?
set X 2
set Y {$X}
set Z VAR-{$X}
In the last set statement there are just two arguments even though your browser may show more.

Solution

 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.