Tcl script 笔记

来源:互联网 发布:网络推广都有哪些网站 编辑:程序博客网 时间:2024/05/16 17:13
  • Another common use of backslashes is to continue long commands on multiple lines. A backslash as the last character in a line is converted into spaces. In addition, all the white space at the beginning of the next line is also absorbed by this substitution.

 

  • Procedure names and variable names do not conflict with each other. And  the name is case sensitive.

 

  • Square brackets, [ ], causes command substitution. Everything between the brackets is treated as a command, and everything including the brackets is replaced with the result of the command. Nesting is allowed.

 

  • Switch

There are four possible flags that determine how value is matched.

-exact     Match the value exactly to one of the patterns. (The default) 精确匹配

-glob      Use glob-style pattern matching. 利用通配符匹配

-regexp  Use regular expression pattern matching. 正则表达式匹配

--            No flag(or end of flags). Useful when value can begin with -.

 

  • The basic data structure in Tcl is string. In addition, there are two higher-level data structures, lists and arrays. List are implemented as strings. Their structure is defined by the syntax of the string. The syntax rules are the same as for commands, and in fact commands are just a articular instance of lists. Arrays are variables that have an index. The index is a string value, so you can think of arrays as maps from one string (the index) to another string (the value of the array element).

 

  • The existence of a variable can be tested with the info exists command. For example, because incr requires that a variable exists, you might have to test for the existence of the variable first.

If ![info exists foobar] {

Set foobar 0

           } else {

                Incr foobar

           }

 

  • llength {a b {c d} “e f g” h}

ð      5

ð      {c d} and “e f g” just like a sublist.

 

  • The stylized placement of the opening curly brace at the end of the first and third line exploits this property to extend the if command over multiple lines.

 

  • Catch returns 0 if there was no error caught, or 1 if it did catch an error.

 

  • The parameter list for a procedure can include default values for parameters.

 

       proc p2 {a {b 7} {c -2}} {

               expr $a / $b + $c

       }

              p2  6  3

              => 0            

 

             {b 7}   7 is the default value of b

            At least one argument and no more than three arguments can be passed to     

              p2.

 

  • A useful trick is to collect your global variables into an array so that it is easier to manage your global statements.

 

  • Double quotes, like braces, are used to group words together. The difference between double quotes and curly braces is that quotes allow substitutions to occur in the group, while curly braces prevent substitutions.

 

  • Tk中的主窗口的名字是”.”

 

  • eval exec [list [file join $env(GENESIS_HOME) Pic pmt stdlib PCA PCAGateway] $PCA_PORT $MB_GROUP &]

参数&的作用是让PCAGateway在后台运行,如果不加这个参数,主程序会进入挂起状态等待PCAGateway执行完毕,从而影响PIC的正常运行

 

  • read --- Read from a channel

参数 –nonewline表示放弃从channel读取的信息的最后的代表新行的符号

read –nonewline channeled

如果没有-nonewline参数,则会分两次从channel中读取信息,第一次是传入的字符串信息,二是表示new line的字符(系统添加)

 

  • TCL的每一个命令包含一个或几个单词,第一个单词代表命令名,另外的单词则是这个命令的参数,单词之间必须用空格或TAB键隔开。

TCL解释器对一个命令的求值过程分为两部分:分析和执行。在分析阶段,TCL 解释器运用规则把命令分成一个个独立的单词,同时进行必要的置换(substitution) 在执行阶段,TCL 解释器会把第一个单词当作命令名,并查看这个命令是否有定义,如果有定义就激活这个命令对应的C/C++过程,并把所有的单词作为参数传递给该命令过程,让命令过程进行处理。

 

  • set y [expr x+10]

如果在上例中我们去掉[],那么TCL会报错。因为在正常情况下,TCL解释器只把命令行中的第一个单词作为看作命令,其他的单词都作为普通字符串处理,看作是命令的参数。

[]中脚本的值为最后一个命令的返回值。

 

  • TCL语言中的反斜杠置换类似于C语言中反斜杠的用法

主要用于在单词符号中插入诸如换行符、空格、[$等被TCL解释器当作特殊符号对待的字符。

 

          set msg multiple/ space //msg的值为multiple space

如果没有'/'的话,TCL会报错,因为解释器会把这里最后两个单词之间的空格认为是分隔符,于是发现set命令有多于两个参数,从而报错。加入了'/'后,空格不被当作分隔符,'multiple space'被认为是一个单词(word)

  • TCL解释器对双引号中的各种分隔符将不作处理,但是对换行符 及$和[]两种置换符会照常处理。

              而在花括号中,所有特殊字符都将成为普通字符,失去其特殊意义,TCL解释器不会对其 

              作特殊处理。

 

  • #必须出现在TCL解释器期望命令的第一个字符出现的地方,才被当作注释。因为它出现在命令的中间,TCL解释器把它和后面的字符当作命令的参数处理,从而导致错误

 

  • TCL中的set命令能生成一个变量、也能读取或改变一个变量的值

 

  • 数组是一些元素的集合。TCL的数组和普通计算机语言中的数组有很大的区别。在TCL中,不能单独声明一个数组,数组只能和数组元素一起声明。

 

  • TCL表达式的操作数通常是整数或实数。整数一般是十进制的, 但如果整数的第一个字符是0(zero),那么TCL将把这个整数看作八进制的,如果前两个字符是0x则这个整数被看作是十六进制的。

 

  • list这个概念在TCL中是用来表示集合的。TCLlist是由一堆元素组成的有序集合,list可以嵌套定义,list每个元素可以是任意字符串,也可以是list

 

  • 在一个proc中使用已定义的全局变量,必须在使用前用global声明,否则无法使用

 

  • catch命令可以有第二个参数。如果提供这个参数,它应该是一个变量名,catch把脚本

的返回值或者是出错信息存入这个变量。

%catch {unset x} msg

1

%set msg

can't unset "x": no such variable

 

<script type="text/javascript"><!--google_ad_client = "pub-0068659036624865";/* 468x60, 创建于 09-6-8 */google_ad_slot = "2787955370";google_ad_width = 468;google_ad_height = 60;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

原创粉丝点击