3/25/2006 10:11:08 PM
2
目录
Tcl ............................................................................5
TCL
语法
..................................................................5
■脚本、命令和单词符号.......................................................................................................5
■置换(substitution) .................................................................................................................6
■变量置换(variable subtitution) .....................................................................................6
■命令置换(command substitution).................................................................................6
■反斜杠置换(backslash substitution).............................................................................7
■双引号和花括号...........................................................................................................8
■注释.......................................................................................................................................9
变量
......................................................................10
■简单变量.............................................................................................................................10
■数组.....................................................................................................................................11
■相关命令.............................................................................................................................12
■set................................................................................................................................12
■unset............................................................................................................................12
■append和incr...............................................................................................................12
表达式
...................................................................14
■操作数.................................................................................................................................14
■运算符和优先级.................................................................................................................15
■数学函数.............................................................................................................................16
List......................................................................18
■list命令................................................................................................................................18
■concat命令 ..........................................................................................................................19
■lindex命令...........................................................................................................................20
■llength命令 .........................................................................................................................21
■linsert命令...........................................................................................................................22
■lreplace命令........................................................................................................................23
■lrange 命令.........................................................................................................................24
■lappend命令 ........................................................................................................................25
■lsearch 命令 .......................................................................................................................26
■lsort命令 .............................................................................................................................27
■split命令..............................................................................................................................28
■join命令 ..............................................................................................................................29
控制流
...................................................................30
■if命令 ..................................................................................................................................30
■循环命令:while 、for、 foreach ...................................................................................31
3/25/2006 10:11:08 PM
3
■while命令....................................................................................................................31
■for命令........................................................................................................................31
■foreach命令.................................................................................................................31
■break和continue命令..................................................................................................32
■switch 命令 ................................................................................................................32
■eval命令..............................................................................................................................34
■source命令 ..........................................................................................................................35
过程
(procedure)...................................................36
■过程定义和返回值.............................................................................................................36
■局部变量和全局变量.........................................................................................................37
■缺省参数和可变个数参数.................................................................................................38
■引用:upvar........................................................................................................................39
字符串操作
.............................................................40
■format命令..........................................................................................................................40
■scan命令 .............................................................................................................................41
■regexp命令..........................................................................................................................42
【TCL正则表达式规则详细说明】 .............................................................................44
■regsub命令..........................................................................................................................55
■string命令 ...........................................................................................................................56
■1、string compare ?-nocase? ?-length int? string1 string2.........................................56
■2、string equal ?-nocase? ?-length int? string1 string2..............................................56
■3、string first string1 string2 ?startindex?..................................................................56
■4、string index string charIndex.................................................................................56
■5、string last string1 string2 ?startindex?...................................................................57
■6、string length string.................................................................................................57
■7、string match ?-nocase? pattern string....................................................................57
■8、string range string first last....................................................................................58
■9、string repeat string count.......................................................................................58
■10、string replace string first last ?newstring?...........................................................58
■11、string tolower string ?first? ?last?........................................................................58
■12、string toupper string ?first? ?last? .......................................................................58
■13、string trim string ?chars?.....................................................................................58
■14、string trimleft string ?chars?................................................................................59
■15、string trimright string ?chars? .............................................................................59
文件访问
................................................................60
■文件名.................................................................................................................................60
■基本文件输入输出命令.....................................................................................................61
■随机文件访问.....................................................................................................................63
■当前工作目录.....................................................................................................................64
■文件操作和获取文件信息.................................................................................................65
错误和异常
.............................................................69
3/25/2006 10:11:08 PM
4
■错误.....................................................................................................................................69
■从TCL脚本中产生错误 .....................................................................................................71
■使用catch捕获错误 ............................................................................................................72
■其他异常.............................................................................................................................73
深入
TCL ................................................................75
■查询数组中的元素.............................................................................................................75
■info命令 ..............................................................................................................................77
■变量信息.....................................................................................................................77
■过程信息.....................................................................................................................78
■命令信息.....................................................................................................................79
■TCL的版本和库 .........................................................................................................79
■命令的执行时间.........................................................................................................79
■跟踪变量.....................................................................................................................80
■命令的重命名和删除.................................................................................................82
■unknown命令..............................................................................................................83
■自动加载.....................................................................................................................83
3/25/2006 10:11:08 PM
5
Tcl
TCL
语法
■脚本、命令和单词符号
一个 TCL 脚本可以包含一个或多个命令。命令之间必须用换行符或分号隔开,下面的两个脚本
都是合法的:
set a 1
set b 2
或
set a 1;set b 2
TCL 的每一个命令包含一个或几个单词,第一个单词代表命令名,另外的单词则是这个命令的
参数,单词之间必须用空格或 TAB 键隔开。
TCL 解释器对一个命令的求值过程分为两部分:分析和执行。在分析阶段,TCL 解释器运用规
则把命令分成一个个独立的单词,同时进行必要的置换(substitution); 在执行阶段,TCL 解
释器会把第一个单词当作命令名,并查看这个命令是否有定义,如果有定义就激活这个命令对应
的 C/C++过程,并把所有的单词作为参数传递给该命令过程,让命令过程进行处理。