1
3/25/2006
10:11:08 PM
Tcl
教程
[Edit
by
roben_chen]
http://2316.vip.nease.net/scriptnet/ssdn/index.htm
2
3/25/2006
10:11:08 PM
目½
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
3/25/2006
10:11:08 PM
■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
4
3/25/2006
10:11:08 PM
■错误.....................................................................................................................................69
■从TCL脚本中产生错误
.....................................................................................................71
■½用catch捕获错误
............................................................................................................72
■其他异常.............................................................................................................................73
深入
TCL
................................................................75
■查询数组中的元素.............................................................................................................75
■info½令
..............................................................................................................................77
■变量信息.....................................................................................................................77
■过程信息.....................................................................................................................78
■½令信息.....................................................................................................................79
■TCL的版本和库
.........................................................................................................79
■½令的执行时间.........................................................................................................79
■跟踪变量.....................................................................................................................80
■½令的重½名和删除.................................................................................................82
■unknown½令..............................................................................................................83
■自动加½½.....................................................................................................................83
5
3/25/2006
10:11:08 PM
Tcl
TCL
语法
■脚本、½令和单词符号
一个
TCL
脚本可以包含一个或多个½令。½令之间必须用换行符或分号隔开,下面的两个脚本
½是合法的:
set a 1
set b 2
或
set a 1;set b 2
TCL
的每一个½令包含一个或几个单词,第一个单词代表½令名,另外的单词则是这个½令的
参数,单词之间必须用空格或
TAB
键隔开。
TCL
解释器对一个½令的求值过程分为两部分:分析和执行。在分析阶段,TCL 解释器运用规
则把½令分成一个个独立的单词,同时进行必要的½换(substitution); 在执行阶段,TCL 解
释器会把第一个单词½½½令名,
并查看这个½令是否有定义,
如果有定义就激活这个½令对应
的
C/C++过程,并把所有的单词½为参数传递给该½令过程,让½令过程进行处理。
评论