23

I'm learning Tcl/Tk and am confused on the usage of curly braces in tcl.

To me it seems to be used to both indicate scope and declare strings! Is this a bug (or feature)?

Is my interpretation correct?

Jerry
  • 70,495
  • 13
  • 100
  • 144
jack the lesser
  • 701
  • 2
  • 7
  • 15

4 Answers4

29

In a nutshell,

  • Tcl's braces act like sh's single quotes -- group words (and lines) without interpolation.
  • Tcl's double quotes act like sh's double quotes -- allowing interpolation.

The fact that you use curly braces in a proc definition is not mandatory. It's just the most convenient way to pass a script as an argument to proc without interpolating.

These are equivalent

proc add3 {a b c} {
    return [expr {$a + $b + $c}]
}

and

proc add3_weird [list a b c] "return \[expr {\[set a] + \[set b] + \[set c]}]"

Once you internalize Tcl quoting, you'll realize how truly flexible Tcl can be.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 5
    In case it wasn't clear in the answer, interpolation means that substitution will happen. If you have a variable named myvar = "some string", then {a $myvar b} will result in the string literal "a $myvar b"... while "a $myvar b" will result in the string literal "a some string b" – RHSeeger Feb 03 '10 at 19:50
  • thanks, i think i get it now. will have to use tcl a little more to understand fully i guess. – jack the lesser Feb 04 '10 at 04:46
  • 3
    I like to say "you only need to use curly braces when you need to use curly braces", meaning that -- unlike most languages -- curly braces are *only* for quoting. There's nothing in the language that ever requires you use curly braces. They are quoting mechanism which you can choose to use or not. Of course, there are best practices that say when it's wise to use them... – Bryan Oakley Feb 05 '10 at 16:58
  • 2
    I can understand "index", "$index", "{index}", and "{$index}". I recently came across "${index}" and was ... wut?! Ultimately parseable, but why write it like that? – Toddius Zho Aug 16 '13 at 19:51
  • Since I never replied to that comment: use braces for variable substitutions when there is other text that should not be part of the variable name: `set index 4; puts "this is the ${index}th index"` – glenn jackman Nov 09 '18 at 17:31
5

In reply to Toddius Zho's comment, I've seen a few questions (and had them my self) why curly braces are used when using variables e.g. ${var}

In TCL a standard scalar variable can be comprised of any character. However the variable substitution operator "$" assumes you are using alphanumeric characters and underscores (plus name space separators "::")

So if a variable is set with a non alphanumeric character, such as "!", TCL will error when attempting to substitute the variable.

% set myvar! 123
123
% puts $myvar!
can't read "myvar": no such variable

Using curly brackets the substitution operator does not error:

% set myvar! 123
123
%puts ${myvar!}
123

This is because TCL does not perform substitution/interpolation on anything defined within curly braces.

http://www.tcl.tk/man/tcl8.5/TclCmd/Tcl.htm

MrPlastic
  • 51
  • 1
  • 2
-3

curly braces group words together to become arguments. you can find plenty tcl stuffs on the internet by googling. Here's an intro

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
-3

man n Tcl

That is the manual page for the Tcl interpreter! Read it. Read it again! You might even consider to go to a quiet place and read it out loud. This will hinder you from reading too fast. Every single letter counts.

Once you find yourself in "quoting hell" go back to this document and read it again.

There is an online version at: http://www.tcl.tk/man/tcl8.5/TclCmd/Tcl.htm

amucha
  • 33