* Lawrence Woodman <
lorryw...@gmail.com>
| I'm trying to understand the behaviour of subst -nobackslashes depending on
| how a string is quoted. The first line below outputs "!!", while the second
| returns a missing close-bracket error.
>
| puts [subst -nobackslashes "!\[\]!"]
When using "" for quoting, the TCL parser "looks inside" the "" and
already does one round of substitution of whatever can be substituted.
So the \ never makes it to the 'subst' command:
puts "!\[\]!"
=> ![]!
This ![]! is what subst sees (note: no backslashes). The [] then is
replaced by subst by the empty string, since there was no command given
to call.
| puts [subst -nobackslashes {B: \[\]}]
When using {} for qouting, the enclosed text is used as-is, no
substition performed by the parser:
puts {B: \[\]}
=> B: \[\]
So subst 'sees' "B: \[\]" (with backslashes).
puts [subst -nobackslashes {B: \[\]}]
=> ERR missing close-bracket
I understand it like this:
Since subst was instructed to *not* handle backslashes, the first "\"
loses its special meaning, and the following "[" starts a command to call.
Now the parser jumps in again and handles the rest: the "\]" is handled
by the parser and since escaped, the "]" does *not* terminate the [.
But then the string ends and there is no corresponding ] found,
hence the error.
| Any help to understand this would be most appreciated.
HTH, and hopefully my interpretation is not wrong in terms of TCL
internals :-)
R'