Tcl/Tk Notes (1)

Tcl/Tk Notes



Part I TCL Overview

1 Everything Is a String

In Tcl, evrything is a string, and you have to explicitly ask for evaluation of variables and nested commands.

2 Substitution

  • Variable
The use of dollar sign($) is one kind of substitution. Variables can be assigned values by set command.

  • Command Substitution
A nested command is delimited by square brackets, [ and ]. The Tcl takes everything between the brackets and evaluates it as a command. It Reqrites the outer command by replacing the square brackets and everything between them with the result of nested command.

  • Math Expression
The expr command is used to evaluate math expressions. The Tcl interpreter treats expr just like any other command, and it leaves the expression parsing up to the expr implementation.

  • Backslash Substitution
Backslash(/) is used to quote characters that have special meaning to the interpreter.
You can also specify characters that are hard to type directly by giving their octal or hexadecimal value.
Another common use of backslashes is to continue long commands on multiple lines. A backslash as the last character in a line is converted into a space.

3 Double Quotes vs. Curly Braces

Double quotes, like braces, are used to group words together. The difference between double quotes and curly braces is that quotes allow substitutions to occur in the group, while curly braces prevent substitutions.

In practice, grouping with curly braces is used when substitutions on the argument need to be delayed until a later time( or never done at all). Examples include contrl flow statements and procedure declararions. Double quotes are usefulin simple cases like the puts command.
Another common use of quotes is with the format command that is similar to C printf function. The only way to effectively group special characters in format command into a single argument is with quotes.
e.g. puts [format "Item: %s/t%5.3f" $name $value]

4 Procedures

Tcl uses the proc command to define procedures. The syntax is :
proc name arglist body
The return command in Tcl procedure is optional because the interpreter will return the value of the last command in the body as the value of the procedure.

5 A While Loop Example


The loop around boolean expression $i<=10 is crucial, because it delays substitution until while command implementation tests the expression. The following expression is an infinite loop:
set i; while $i<=10 {incr i}

The Tcl interpreter will substitute for $i before while is called, so while gets a constant expression 1<=10 that will always be ture.You can avoid this kind of errors by adopting a consistent coding style that always groups expressions and command bodies with curly braces.
The command in the example uses set with a single argument. When used in this way the set command returns the current value of the named variable.

6 Grouping And Command Substitution

Grouping decisions are made before substitutions are performed. This means that the values of variables or command results do not affect grouping.
A single round if substitutions is performed before command invocation. That is, the result of a substitution is not interpreted a second time. This rule is important if you have a variable value or a command result that contains special characters such as spaces, dollar-signs, square brackets or braces. Because only a single round of substitution is done, you don't have to worry about special characters in values causing extra substitutions.

7 Comments

Tcl uses # for comments. Unlike many languages, the # character must occur at the begining of a command. An easy trick to append a comment to the end of a command is to proceed the # with a semicolon in order to terminate the previous command.
A backslash(/) continues a comment line onto the next line of the script. In addition, a semi-colon inside a comment is not significant. only a newline terminates comments.

8 Reference

  • backslash Sequences

  • Arithmetic Operators
  • Built-in Math Function
  • Core Tcl Commands


  • Predefined Variables


















你可能感兴趣的:(Note)