XPS-Q8
Tcl Manual
•
Grouping decisions
are made before substitutions are performed, which means that
the values of variables or command results do not affect grouping.
•
A
dollar sign
,
$
, causes variable substitution. Variable names can be any length, and
are sensitive to case used. If variable references are embedded into other strings, or
if they include characters other than letters, digits, and the underscore, they can be
distinguished with the
${
varname
}
syntax.
•
Square
brackets
,
[ ]
, cause command substitution. Everything between the
brackets is treated as a command, and everything including the brackets is replaced
with the result of the command. Nesting is allowed.
•
The
backslash character
,
\
, is used to quote special characters. You can think of this
as another form of substitution in which the backslash and the next character or
group of characters is replaced with a new character.
•
Substitutions
can occur anywhere unless prevented by curly brace grouping. Part of
a group can be a constant string, and other parts can be the result of substitutions.
Even the command name can be affected by substitutions.
•
A single round of substitutions is performed before a command is invoked. 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 do not have to worry about special characters in values
causing extra substitutions.
2.2.13
Fine Points
•
A common error is to forget a space between arguments when grouping with braces
or quotes. This is because white space is used as the separator, while the braces or
quotes only provide grouping. If you forget the space, you will get syntax errors about
unexpected characters after the closing brace or quote. The following is an error because
of the missing space between
}
and
{
:
if
{$x >
1
}{
puts
"x = $x"
}
•
A double quote is only used for grouping when it comes after white space. This
means you can include a double quote in the middle of a group without quoting it
with a backslash. This requires that curly braces or white space delimit the group.
Using this obscure feature is not recommended, but this is what it looks like:
set
silly a"b
•
When double quotes are used for grouping, the special effect of curly braces is
turned off. Substitutions occur everywhere inside a group formed with
double
quotes.
set
x xvalue
set
y
"foo {$x} bar"
⇒
foo {xvalue} bar
•
When double quotes are used for grouping and a nested command is encountered,
the nested command can use double quotes for grouping, also.
puts
"results [format "
%f %f
" $x $y]"
•
Spaces are
not
required around the square brackets used for command
substitution. For the purposes of grouping, the interpreter considers everything
between the square brackets as part of the current group. The following sets
x
to the
concatenation of two command results because there is no space between
]
and
[
.
set
x [
cmd1
][
cmd2
]
•
Newlines and semicolons are ignored when grouping with braces or double quotes.
They get included in the group of characters just like all the others. The following
sets
x
to a string that contains newlines:
set
x
"This is line one.
EDH0307En1041 — 10/17
13