Evaluation & Substitutions 3: Grouping arguments with []

Table of Contents

Evaluation & Substitutions 3: Grouping arguments with []
Example

Evaluation & Substitutions 3: Grouping arguments with []

You obtain the results of a command by placing the command in square brackets ([]). This is the functional equivalent of the back single quote (`) in sh programming, or using the return value of a function in C.

As the Tcl interpreter reads in a line it replaces all the $variables with their values. If a portion of the string is grouped with square brackets, then the string within the square brackets is evaluated as a command by the interpreter, and the result of the command replaces the square bracketed string.

Let's take the following code segment, for example:


<para><code>puts [readsensor [selectsensor]]</code>
</para>

  • The parser scans the entire command, and sees that there is a command substitution to perform:

    readsensor [selectsensor] , which is sent to the interpreter for evaluation.

  • The parser once again finds a command to be evaluated and substituted,

    selectsensor

  • The fictitious

    selectsensor command is evaluated, and it presumably returns a sensor to read.

  • At this point, readsensor has a sensor to read, and the readsensor command is evaluated.

  • Finally, the value of readsensor is passed on back to the

    puts command, which prints the output to the screen.

The exceptions to this rule are as follows:

  • A square bracket that is escaped with a \ is considered as a literal square bracket.

  • A square bracket within braces is not modified during the substitution phase.