Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

tcl - Understanding the usage of braces

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...