TCL 双引号和花括号的区别

用了很一段时间的Modelsim,用TCL脚本仿真,开始注意起来它,看了些资料,有一个问题始终让我很困惑,那就是花括号 的用法,今天Google了一下,觉得有一点豁然了,记录一下。为了不让自己的翻译影响大家理解,先给出原文,再翻译一下。

第一段:

THE RULE

SQUARE BRACKETS are used to define a block that's run BEFORE the rest of the command on the current line, and the result is substituted into the line.

CURLY BRACES are used to define a block that's deferred - in other words it may be run AFTER the rest of the command on the current line.

规则:

方括号是用来定义一个块,它在当前行的其余命令之前运行,并且结果替换到当前行中。

花括号是用来定义一个块,它是延期的,换句话说它会在当前行的其余命令之后运行。

第二段:

In Tcl, both Curley braces and double quotes can be used to hold a block of program or data together as a single unit / parameter ... but there are differences .

a) Curley braces can stretch over a number of lines, with new lines within the block being simply a part of the block. So they're ideal for defining blocks of code.

b) Curley braces can be nested - since there are different open and close characters, blocks within blocks are written easily and naturally, which is quite impractical with double quotes!

c) the biggest difference is that double quoted blocks are evaluated at the time they are encountered by the language parser, but curley braces are deferred until they are (perhaps) evaluated later under the control of the command of which they form a part.

在TCL中,花括号和双引号都可以用来把一块程序或数据组合起来成一个单元(参数),但是他们的用法有区别。

a)     花括号可以延续到若干行,可以在块内包含若干的新行。所以花括号很适合用来定义代码块。

b)     花括号可以嵌套。因为花括号左(open)右(close)不同的字符表示,所以块中嵌套的块可以被容易自然的书写,这时使用双引号是不实际的。

c)     最大的不同是TCL语言解释器在遇到双括号内的块时就进行估值,但是花括号是延期的,块在由它形成的不同的命令的控制下延迟估值。(翻译不太好,看原文)

看下面的例子

set sample 5

proc demo {} {global sample; return $sample}

proc omed {} "global sample; return $sample"

 set sample 27

 puts "Curley braces - defer substitution until block is run"

puts [demo]

puts "Double quotes - just grouping; substition at definition time"

puts [omed]

 puts [info body demo]

puts [info body omed]

       很多资料里说到花括号就是它阻止替换,但是在定义的过程中,如果不替换那么块中的命令根本无法执行。这就很矛盾。

         其实,花括号也会替换,只是延迟了,不是很多地方说的不替换,也没有什么例外。只是这个延迟我们得好好体会一下。

         顺便说一下,在有些资料里说TCL中的流程控制语句是说TCL对花括号的要求很严格之类的说法,其实TCL的“保留字”都是命令,命令和参数之间肯定得有空格,而且要换行又要保持命令的格式就得用花括号了,所以对于for之类的命令换行前都得有个open braces。

原文地址:https://www.cnblogs.com/hfyfpga/p/4254875.html