Understanding the usage of braces

tcl
jack the lesser picture jack the lesser · Feb 3, 2010 · Viewed 18.5k times · Source

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?

Answer

glenn jackman picture glenn jackman · Feb 3, 2010

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.