Tcl
编程初步
编辑版本
日
期
著
者
: 1.5
:2004-3-2
:陈 涛
上海贝尔阿尔卡特有限公司
Tcl
编程初步
总目½
总目½...............................................................................................................................................
I
例索引.............................................................................................................................................
IV
表索引.............................................................................................................................................
VI
图索引............................................................................................................................................
VII
第
1
章.
TCL
基本知识................................................................................................................
1
1.1
什么是
T
CL
..............................................................................................................................1
1.2
T
CL
自学工具...........................................................................................................................1
1.3
T
CL
½件包
..............................................................................................................................2
1.4
T
CL
½令格式...........................................................................................................................2
1.5
T
CL
脚本文件和
SOURCE
½令
..................................................................................................3
1.6
可执行脚本文件(E
XECUTABLE
F
ILE
) ...........................................................................................3
1.7
获得帮助.................................................................................................................................4
1.7.1
Windows
系统
.............................................................................................................4
1.7.2
Unix
系统
......................................................................................................................4
第
2
章.
输出、赋值与替换
........................................................................................................ 5
2.1
PUTS
.........................................................................................................................................5
2.2
SET
&
UNSET
..............................................................................................................................5
2.3
替换
........................................................................................................................................6
2.3.1
$...................................................................................................................................6
2.3.2
[] ..................................................................................................................................6
2.3.3
" "
和
{} ..........................................................................................................................6
2.3.4
\...................................................................................................................................7
第
3
章.
3.1
3.2
3.3
3.4
第
4
章.
数学表达式与
EXPR
½令
.............................................................................................. 8
数学和逻辑运算符..................................................................................................................8
数学½数.................................................................................................................................8
数学运算举例
.........................................................................................................................9
INCR
½令
................................................................................................................................9
字符串
........................................................................................................................ 10
4.1
基本½令集
...........................................................................................................................10
4.2
APPEND
½令..........................................................................................................................10
4.3
FORMAT
½令..........................................................................................................................10
4.3.1
format
½令说明
...................................................................................................... 10
4.3.2
format
举例
.............................................................................................................. 11
4.4
SCAN
½令
.............................................................................................................................12
4.5
BINARY
½令
...........................................................................................................................13
4.6
SUBST
½令..............................................................................................................................14
4.7
STRING
½令
...........................................................................................................................15
4.7.1
string
½令列表
......................................................................................................... 15
4.7.2
字符串比较
................................................................................................................ 16
4.7.3
string match
字符串匹配
......................................................................................... 16
4.7.4
字符串替换
................................................................................................................ 18
4.7.5
字符类别
(class)
测试
................................................................................................. 18
4.7.6
字符串映射
................................................................................................................ 19
i
Tcl
编程初步
第
5
章.
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12
第
6
章.
TCL
列表操½..............................................................................................................
20
列表½令集
...........................................................................................................................20
LIST
½令.................................................................................................................................20
CONCAT
½令.........................................................................................................................21
LAPPEND
½令
........................................................................................................................21
LLENGTH
½令
.........................................................................................................................22
LINDEX
½令............................................................................................................................22
LRANGE
½令..........................................................................................................................22
LINSERT
和
LREPLACE
½令........................................................................................................22
LSEARCH
½令.........................................................................................................................23
LSORT
½令
.............................................................................................................................24
JOIN
与
SPLIT
½令...................................................................................................................24
FOREACH
控制结构
................................................................................................................27
数组............................................................................................................................
28
6.1
数组的定义与格式................................................................................................................28
6.2
数组变量...............................................................................................................................29
6.3
多维数组...............................................................................................................................29
6.4
数组操½½令
.......................................................................................................................30
6.4.1
array get
½令
......................................................................................................... 30
6.4.2
array names
½令
.................................................................................................... 30
6.4.3
遍历数组
................................................................................................................... 31
6.4.4
用数组定义结构
........................................................................................................ 31
第
7
章.
7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.9
第
8
章.
控制结构½令
............................................................................................................. 32
IF
/
ELSE
½令............................................................................................................................32
FOR
½令................................................................................................................................33
WHILE
½令
.............................................................................................................................33
........................................................................................................34
SWITCH
½令
...........................................................................................................................34
CATCH
½令
...........................................................................................................................35
ERROR
½令
............................................................................................................................36
RETURN
½令
...........................................................................................................................37
EXIT
½令
................................................................................................................................37
BREAK
与
CONTINUE
½令
过程与½用域
............................................................................................................. 38
8.1
PROC
—过程定义½令 ...........................................................................................................38
8.2
½用域...................................................................................................................................40
8.2.1
过程的½用域
............................................................................................................ 40
8.2.2
变量的½用域
............................................................................................................ 40
8.3
UPVAR
½令............................................................................................................................42
8.4
RENAME
½令
.........................................................................................................................44
8.5
特殊变量...............................................................................................................................45
8.5.1
½令行参数
................................................................................................................ 45
8.5.2
env--
环境变量数组
................................................................................................... 45
8.6
EVAL
½令
..............................................................................................................................46
8.7
UPLEVEL
½令
..........................................................................................................................48
第
9
章.
9.1
9.2
9.3
正则表达式(REGULAR
EXPRESSIONS) ................................................................... 49
..........................................................................................................................49
REGSUB
½令...........................................................................................................................51
正则表达式的语法................................................................................................................52
REGEXP
½令
ii
Tcl
编程初步
9.3.1
9.3.2
9.3.2.1
9.3.2.2
9.3.2.3
9.3.2.4
9.3.2.5
9.3.2.6
9.3.2.7
9.3.2.8
分支
(branch)
和原子
(atom).................................................................................... 52
基本语法
................................................................................................................... 52
匹配字符........................................................................................................................................52
限定匹配........................................................................................................................................53
方括号表达式与字符集
.................................................................................................................53
匹配分支........................................................................................................................................54
量词(Qulifier)
...........................................................................................................................54
子模式与匹配报告捕获
.................................................................................................................55
反斜杠引用....................................................................................................................................56
匹配优先级....................................................................................................................................56
9.3.3
9.3.3.1
9.3.3.2
9.3.3.3
9.3.3.4
9.3.3.5
9.3.3.6
9.3.3.7
9.3.3.8
9.3.3.9
9.3.3.10
9.3.3.11
高级正则表达式(
AREs
)
......................................................................................... 57
反斜杠换码(escape)序列............................................................................................................57
½整元素(collating
element) .....................................................................................................57
等价类(equivalence
class)........................................................................................................58
字符类(character
class).............................................................................................................58
非贪婪量词....................................................................................................................................59
约束量词........................................................................................................................................59
回退引用........................................................................................................................................59
前瞻(lookahead).........................................................................................................................60
换行符敏感的匹配.........................................................................................................................60
嵌入式选项
...............................................................................................................................60
扩展语法
...................................................................................................................................60
9.3.4
9.3.5
第
10
章.
10.1
10.2
10.3
10.4
10.5
10.6
第
11
章.
语法小结
................................................................................................................... 60
其它支持正则表达式的½令
...................................................................................... 60
名字空间.................................................................................................................
62
创建名字空间
...................................................................................................................62
用::限定符来½用变量和过程...........................................................................................62
名字空间的变量
...............................................................................................................63
过程的进口与出口............................................................................................................64
内省(
INTROSPECTION
) .........................................................................................................65
名字空间½令集
...............................................................................................................65
跟踪与调试
............................................................................................................. 67
11.1
CLOCK
½令
...........................................................................................................................67
11.1.1
clock clicks
½令
...................................................................................................... 67
11.1.2
clock seconds
½令
................................................................................................ 67
11.1.3
clock format
½令
................................................................................................... 68
11.1.4
clock scan
½令
....................................................................................................... 70
11.2
INFO
½令...............................................................................................................................71
11.2.1
info level .................................................................................................................. 72
11.2.2
info exists ................................................................................................................. 72
11.3
TRACE
½令
............................................................................................................................73
11.3.1
trace variable......................................................................................................... 73
11.3.2
trace vdelete.......................................................................................................... 75
11.3.3
trace vinfo............................................................................................................... 75
第
12
章.
脚本库与½件包......................................................................................................
76
12.1
声明和½用½件包............................................................................................................76
12.1.1
½件包定½
................................................................................................................ 76
12.1.2
声明½件包½令
........................................................................................................ 76
12.1.3
加½½½件包½令
........................................................................................................ 76
12.1.4
自动加½½与½件包索引
............................................................................................. 77
12.1.5
用链接库提供½件包
................................................................................................. 79
iii
Tcl
编程初步
12.2
12.3
第
13
章.
PACKAGE
½令集
...................................................................................................................80
小结
..................................................................................................................................80
文件操½与程序调用...............................................................................................
81
13.1
文件操½...........................................................................................................................81
13.1.1
文件
I/O .................................................................................................................... 81
13.1.2
文件系统信息½令
..................................................................................................... 82
13.1.2.1
13.1.2.2
glob
½令
.................................................................................................................................83
file
½令集.................................................................................................................................83
13.2
程序调用...........................................................................................................................85
13.2.1
用
open
½令打开一个进程管道
.............................................................................. 85
13.2.2
用
exec
½令调用程序
.............................................................................................. 87
13.2.3
pid
½令
.................................................................................................................... 88
第
14
章.
套接字与事件驱动编程简介....................................................................................
89
14.1
套接字编程
.......................................................................................................................89
14.1.1
socket
½令
.............................................................................................................. 89
14.1.1.1
14.1.1.2
Client
端
socket
½令..............................................................................................................89
Server
端
socket
½令选项......................................................................................................90
14.1.2
用
fconfigure
配½套接字
........................................................................................ 90
14.1.3
C/S
编程举例
............................................................................................................ 90
14.2
事件驱动编程
...................................................................................................................93
14.2.1
after
½令
................................................................................................................. 93
14.2.2
fileevent
½令
.......................................................................................................... 96
14.2.3
vwait
½令
................................................................................................................ 96
14.2.4
fconfigure
½令
....................................................................................................... 97
14.2.4.1
14.2.4.2
14.2.4.3
fconfigure
语法
.......................................................................................................................97
非阻塞
I/O ................................................................................................................................98
缓冲
..........................................................................................................................................98
参考文献
...................................................................................................................................... 100
例索引
例
1-1
输出一段字符串的例子........................................................................................................
2
例
1-2 source
½令的简单例子
...................................................................................................... 3
例
2-1
输出一个词的例子
............................................................................................................... 5
例
2-2
参数定义、赋值与参数值引用.............................................................................................
5
例
2-3
嵌套$用做替换操½
............................................................................................................. 6
例
2-4
½令替换[]的例子
............................................................................................................... 6
例
2-5 {}替换的例子 ........................................................................................................................ 6
例
2-6 \的例子................................................................................................................................ 7
例
2-7
不规范的续行......................................................................................................................
7
例
3-1
数学运算举例.......................................................................................................................
9
例
4-1 append
½令的简单例子
................................................................................................. 10
例
4-2
½½说明符的例子
............................................................................................................. 11
例
4-3 format
½令的简单例子
.................................................................................................... 11
例
4-4 scan
½令的简单例子
....................................................................................................... 12
例
4-5
½验
binary format
和
binary scan
的½用....................................................................
13
例
4-6 binary
½令的简单应用
..................................................................................................... 14
例
4-7 subst
½令的简单例子
....................................................................................................... 14
例
4-8 string compare
和
string equal
进行字符串比较的例子...............................................
16
iv
评论