Tcl uses a newline (and semicolon) as a command delimiter. This is the main part of the basic syntax; you cannot get around it, so you need to use double quotes or curly braces to avoid backslash. Let's look at the possibilities (remember, list delimiters can be any non-empty whitespace sequence).
Awful, prone to list backslash errors
set pets [list \ cat \ dog \ $elephant \ ]
No braces with curly braces
set pets { cat dog $elephant }
(Note that above, $elephant is just a sequence of characters, not a read variable.)
With double quotes, replacements, but be careful!
set pets " cat dog $elephant "
"Be careful!" I mean, if you have a verbose member of a list, you need an internal [list β¦] or other quotation:
set pets " cat dog [list $elephant] [list "elephant child"] "
But that would be true with list + backslashes at the top.
Using subst ...
set pets [subst { cat dog $elephant "elephant child" }]
I could "clear this" (and avoid other potential problems) with
set pets [list {*}[subst { cat dog [list $elephant] [list "elephant child"] }]]
But honestly, if things get really complicated, I actually do this:
Build with a few commands
set pets { cat dog } lappend pets $elephant "elephant child"
It makes no sense to beat yourself above your head to use one command when two or more will do everything with fewer problems.
Donal fellows
source share