| 2.7 Command Substitution |
|
|
So far, you have seen that procedures and commands are invoked with lines of the form, command ARG1 ARG2 ... ARGnand that their return values are thrown away. To use the return value, invoke the command with this syntax:
[command ARG1 ARG2 ... ARGn]within another command line. This form is useful anyplace where the return value will be useful. The square brackets cause a form of substitution to happen. First, the command line inside the square brackets is executed. Second, the result of that command is substituted in place of the square brackets. This process is called For example, when executing the command, set X [set Y 0]there will be a substitution phase for the overall command line. During this phase the command line, set Y 0is executed and its value replaces everything in the square brackets. After that substitution, Y contains 0 and the overall command line looks like: set X 0This latter form is immediately executed. The overall effect is to set both X and Y to 0.
Now, we are ready to finish the while command in the file copying example. As a reminder, it looks like this.
while {-1 != the value obtained by executing gets $InFile Line} {
puts $OutFile $Line
}
Try to figure out what the pseudostep should be before reading the answer which comes next. The pseudostep will use command substitution to execute a procedure which reads an input line. The result of command substitution will be compared with -1 to see if the attempt to read an input line succeeded. So the while statement looks like this:
while {-1 != [gets $InFile Line]} {
puts $OutFile $Line
}
|
Author's Home Page |
|
Order from Amazon. |