For a situation like this, I don't think I'd use
format. The easiest thing to do will be to quote the Javascript chunk using
{} characters, as described above. As I mentioned, quoting with
{} prevents the Tcl interpreter from performing any substitutions on the argument when parsing the command.
As an example:
Code:
set js {var daysInMonth = new Array(12);
daysInMonth[0] = 31;
daysInMonth[1] = 28;
daysInMonth[2] = 31;}
The only potential problem is if you need to include "{" or "}" characters in your Javascript string. If the number of "{" characters and the number of "}" characters in the string are balanced, then you'll be okay:
Code:
set c_code {for (i=0; i < 10; i++)
{
printf("[%d]\n", i);
}}
If the braces don't balance though, it does get a bit tricky to get everything to work out okay:
[tt]%
[ignore]set text {[This] is } a $text}[/ignore]
wrong # args: should be "set varName ?newValue?"[/tt]
Although you can use a backslash in this case to "escape" the "}", the backslash is still treated as a literal character and included as part of the string:
[tt]%
[ignore]set text {[This] is \} a $text}[/ignore]
[ignore][This] is \} a $text[/ignore][/tt]
I'll admit that situations like this aren't pretty in Tcl. You can go back to quoting with
"" and escaping everything:
[tt]%
[ignore]set text "\[This\] is \} a \$text"[/ignore]
[ignore][This] is } a $text[/ignore][/tt]
You can "post-process" the string by mapping the "\}" sequences to "}":
[tt]%
[ignore]set text {[This] is \} a text}[/ignore]
[ignore][This] is \} a text[/ignore]
%
[ignore]set text [string map [list \\\} \}] $text][/ignore]
[ignore][This] is } a text[/ignore][/tt]
Or, you can build up the string piecemeal, with different quoting characters around the pieces:
[tt]%
[ignore]set text {[This] is }[/ignore]
[ignore][This] is [/ignore]
%
append text "\} "
[ignore][This] is } [/ignore]
%
append text {a $text}
[ignore][This] is } a $text[/ignore][/tt]
None of these situations are fun to deal with. But I've actually never needed to handle this situation in any of the Tcl code I've written. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting,
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax