Navigation Logo 2.10  More about Substitution Navigation Logo

 

 

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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:

  • The sequences \{ and \} cannot be used as part of the curly brackets which surround an argument. This is a little surprising for {...\} – you might have thought that because backslash substitution is not happening, the backslash is not noticed. It is.

  • Substitution happens only once. A dollar sign generated with this backslash substitution, \44, does not then generate variable substitution. Square brackets obtained with variable substitution from a string value do not then generate command line substitution. And so on.

  • Although Tcl was oriented solely toward strings that consist of normal ASCII characters, that is changing. With version 8.1 (which is experimental at the time of writing), it will be possible to work with international characters using the unicode coding scheme. To support unicodes, backslash substitution for \u is being implemented. The substitution involves the next four characters which must make a hexadecimal number representing the 16 bits of the unicode. Here is an example where a Danish word is placed in a string.
    % set X VR\u00D8VL
    VRØVL
    
    Do not try this with a version of Tcl earlier than 8.1. By the way, the word is used by Danes to tease foreigners who cannot pronounce their language.

    There are unicoded characters for all the world's major languages. There are also unicodes for such symbols as the smiley face. Major companies such as Microsoft have signed on to use unicode. Codes with the 0 in the first two bytes cover many European languages. The last two bytes of those codes are an earlier 8-bit code known as ISO-8859-1. Read more about unicode in the on-line manual under "Utf" and at the unicode Web site. The exact encoding used by Tcl is UTF-8.

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
 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.