XPS-Q8
Tcl Manual
set
x [
expr
{
sqrt
(
2.0
)}]
At this point the value of
x
is a double-precision floating point value, just as you would
expect. If you do this:
set
two [
expr
$x * $x]
then you may or may not get 2.0 as the result! This is because Tcl will substitute
$x
and
expr
will concatenate all its arguments into one string, and then parse the
expression again. In contrast, if you do this:
set
two [
expr
{$x * $x}]
then
expr
will do the substitutions, and it will be careful to preserve the floating point
value of
x
. The expression will be more accurate and run more efficiently because no
string conversions will be done. The story behind Tcl values is described in more detail
in Chapter 44 on C programming and Tcl.
2.2.11
Comments
Tcl uses the pound character,
#
,
for comments. Unlike the convention used by many
other languages, the
#
must occur at the beginning of a command. A
#
that occurs
elsewhere is not treated as a comment. An easy trick to append a comment to the end of
a command is
to precede the
#
with a
semicolon
to terminate the previous command:
# Here are some parameters
set
rate 7.0
;
# The interest rate
set
months 60
;
# The loan term
One subtle effect to watch for is that a
backslash
effectively continues a comment line
onto the next line of the script. In addition, a
semicolon
inside a comment is not
significant. Only a newline terminates comments:
# Here is the start of a Tcl comment \
and some more of it; still in the comment
A surprising property of Tcl comments is that curly braces inside comments are still
counted for the purposes of finding brace pairs. This inconvenient “feature” was
probably intended to keep the original Tcl parser simple. However, this means that the
following will not work as expected:
# if {boolean expression1} {
if
{boolean expression2} {
some commands
}
The previous sequence results in an extra left curly brace, and probably a complaint
about a missing closed brace at the end of your script! A good technique to comment
out large chunks of code is to put the code inside an
if
block that will never execute:
if
{0} {
unused code here
}
2.2.12
Substitution and Grouping Summary
The following rules summarize the fundamental actions of grouping and substitution
that are performed by the Tcl interpreter before it invokes a command:
•
Command arguments are separated by
white space
, unless arguments are grouped
with
curly braces
or
double quotes
as described below.
•
Grouping with
curly braces
,
{ }
, prevents substitutions. Braces nest. The
interpreter includes all characters between the matching left and right brace in the
group, including newlines, semicolons, and nested braces. The enclosing (i.e.,
outermost) braces are not included in the group’s value.
•
Grouping with
double quotes
,
" "
, allows substitutions. The interpreter groups
everything until another double quote is found, including newlines and semicolons.
The enclosing quotes are not included in the group of characters. A double-quote
character can be included in the group by quoting it with a backslash, (e.g.,
\"
).
EDH0307En1041 — 10/17
12