TCL: How to escape a single brace '{' in a string quoted using braces '{...}'? -


i trying use curly-braces define string { } instead of double-quotes " ", don't have escape several characters (such $, [, ]).

however, running problems when string needs contain single { within it.
know can achieve using double-quoted string , escape {, how using "curly-brace string"?

eg. want puts following string 'proc foo { } {' stdout.

puts "proc foo \{ \} \{" gives me desired output: 'proc foo { } {'

however, puts { proc foo \{ \} \{ } gives me: 'proc foo \{ \} \{' literally printing backslashes.

if skip backslashes, puts { proc foo { } {, complains missing brace.

also, if desired string has matching closing-brace within it, works fine.
puts { proc foo { } { } } gives me expected: 'proc foo { } { }'

what correct way escape single unmatched curly-brace in "curly-brace string"?

sorry, terms of service braced strings require have matching close brace every open brace. way have non-matching braces quote/escape them backslash, no backslash substitutions performed, backslash goes string along lone brace.

that doesn't mean can't construct string want, instance this:

set foo { proc foo { } } # ->  proc foo { }  append foo "{ " # ->  proc foo { } {  

(btw, don't need backslash braces in doublequoted string, " proc foo { } { " fine.)

however, string building seems imply building procedure definition within script, , going wrong way. instead of assembling definition this:

set foo { proc foo { } } # ->  proc foo { }  append foo "{ " # ->  proc foo { } {  append foo { puts foo } # ->  proc foo { } {  puts foo  append foo " }" # ->  proc foo { } {  puts foo  } 

you should this:

set name foo set args {} set body {puts foo} proc $name $args $body 

which defines procedure directly. if need string, wrap this:

set foo [list proc $name $args $body] # -> proc foo {} {puts foo} 

yes, using list construct string. it's because wrapping proc $name $args $body in double quotes lead loss of list structure of command. create list, , use string representation of list string.

if way, you're working with tcl interpretation rules , taking advantage of them, rather working against them. means less work , fewer errors.

documentation: append, list, proc, set


Comments