| |
Besides variable and command substitution, Tcl command lines undergo
backslash substitution. With backslash substitution, a backslash
(\) and some following characters are replaced with something else. Which
following characters and which replacement characters depend on context.
Backslash substitution actually performs several services. With it you can:
- Use characters without triggering their special Tcl meanings. If you
write a backslash before a dollar sign, for example, then the dollar sign is
no longer a signal to perform variable substitution.
For example, \$X
means the two characters $ and X rather than the character \
followed by the value of X.
The character you put after the backslash when you need this case is
nonalphameric.
- Put unprintable characters into Tcl scripts. For example, \n is a
new line character (or characters) and \t is a tab character. There are
only a few such backslash codes.
The character you put after the backslash when you need this case is
a letter.
- A more general way to include an unprintable character is to write a
backslash and follow it with one, two, or three digits. These digits are
taken to represent the position of the character in the ASCII sequence --
using this form, the number must be written in octal. Even printable
characters can be represented this way. For example, \44 is the same
character as \$.
The character you put after the backslash when you need this case is
a digit.
- Make multiple lines seem like one line to the Tcl interpreter. If you
write a backslash before a carriage return, then the next line will be
considered to be a continuation of the line you just ended with exactly
one space in between. You can indent the continuation line and Tcl will
still see just one white space. For example
puts "Hellow\
orld"
causes "Hellow orld" to be printed. This is Tcl's solution to a common
problem of how to enter long strings without messing up your indentation.
This form of backslash substitution happens before anything else. So
the above example would work just as well if curly brackets were used instead
of quotes.
For a complete description of backslash substitution, variable substitution,
and command substitution, look up "Tcl" in your on-line manual. Three more
points are worth making here:
Exercise 2.10a -
What will be output by each of the following
puts statements?
set X Zip
puts "\""
puts {\"}
puts \$X
puts "[set X]{$X}!"
puts {[set X]"$X"}
puts "\
HI"
puts {\
HI}
puts "\44"
puts {\44}
puts one{\44}more\ \}
puts one{\44}more }
set X 1; while {$X} "puts \44X; set X 0"
Solution
Exercise 2.10b -
This is about using Tcl under DOS/Windows, but even those
of you with Unix versions should be able to answer it. Suppose you want to
pass the pathname C:\DOSNAME to the source command. One way is to
replace the backslash with a slash and rely on a nice conversion routine that
the Windows version of Tcl provides for source.
Give two more ways that will work and explain why
source C:\DOSNAME
does not work.
Solution
Some Final Points
Skip this on first reading, if you like.
If you want substitution to occur where it would not otherwise occur, there
is a subst command to do it for you. You are not likely to need
it.
Suppose you want to do variable substitution in a setting where the
character after the variable name seems to be part of the variable name. For
example, you want to write
puts $X1
to mean "print the contents of X followed by the character 1".
One way to solve this problem is to do command substitution, that is,
puts [set X]1
Tcl provides a second solution that makes use of curly brackets.
For example,
puts ${X}1
Both methods work with variable names that contain nonstandard
characters. For example,
% set {bad variable name} 1
1
% puts [set {bad variable name}]
1
% puts ${bad variable name}
1
|
|