TCL语言Tcl(最早称为“工具命令语言”"Tool Command Language", 但是目前已经不是这个含义,不过我们仍然称呼它为TCL)是一种脚本语言。
由John Ousterhout创建。
TCL很好学,功能很强大。
TCL经常被用于快速原型开发,脚本编程,GUI和测试等方面。
TCL念作“踢叩” "tickle". Tcl的特性包括:* 任何东西都是一条命令,包括语法结构(for, if等)。
* 任何事物都可以重新定义和重载。
* 所有的数据类型都可以看作字符串。
* 语法规则相当简单* 提供事件驱动给Socket和文件。
基于时间或者用户定义的事件也可以。
* 动态的域定义。
* 很容易用C, C++,或者Java扩展。
* 解释语言,代码能够动态的改变。
* 完全的Unicode支持。
* 平台无关。
Win32, UNIX, Mac上都可以跑。
* 和Windows的GUI紧密集成。
Tk* 代码紧凑,易于维护。
TCL本身不提供面向对象的支持。
但是语言本身很容易扩展到支持面向对象。
许多C语言扩展都提供面向对象能力,包括XOTcl, Incr Tcl 等。
另外SNIT扩展本身就是用TCL写的。
使用最广泛的TCL扩展是TK。
TK提供了各种OS平台下的图形用户界面GUI。
连强大的Python语言都不单独提供自己的GUI,而是提供接口适配到TK上。
另一个流行的扩展包是Expect. Expect提供了通过终端自动执行命令的能力,例如(pass wd, ftp, telnet等命令驱动的外壳).下面是TCL程序的例子:#!/bin/sh# next line restarts using tclsh in path \exec tclsh ${1+"$@"}# echo server that can handle multiple# simultaneous connections.proc newConnection { sock addr port } {# client connections will be handled in# line-buffered, non-blocking modefconfigure $sock -blocking no -buffering line# call handleData when socket is readablefileevent $sock readable [ list handleData $sock ]}proc handleData {puts $sock [ gets $sock ]if { [ eof $sock ] } {close $sock}}# handle all connections to port given# as argument when server was invoked# by calling newConnectionset port [ lindex $argv 0 ]socket -server newConnection $port# enter the event loop by waiting# on a dummy variable that is otherwise# unused.vwait forever另外一个TK的例子(来自 A simple A/D clock) 它使用了定时器时间,3行就显示了一个时钟。
proc every {ms body} {eval $body; after $ms [info level 0]}pack [label .clock -textvar time]every 1000 {set ::time [clock format [clock sec] -format %H:%M:%S]} ;# RS解释:第一行定义了过程every,每隔ms毫秒,就重新执行body代码。
第二行创建了标签起内容由time变量决定。
第3行中设置定时器,time变量从当前时间中每秒更新一次。
Tcl被广泛的用做script语言,大多数情况下,Tcl和Tk(“Tool Kit”)库同时使用,Tk是一系列令Tcl易于编写图形用户接口的命令和过程Tcl的一个重要特性是它的扩展性。
如果一个程序需要使用某些标准Tcl没有提供的功能,可以使用c语言创造一些新的Tcl命令,并很容易的融合进去。
正是由于Tcl易于扩展,很多人为它编写了扩展包,并在网上共享。
Tcl和其他编程语言例如c不同,它是一种解释语言而非编译语言。
Tcl程序由一系列Tcl命令组成,在运行时由Tcl解释器解释运行。
解释运行的一个优点是它可以自己为自己生成Tcl script。
变量和变量交换不像c,Tcl的变量在使用前不需要声明。
Tcl的变量在它首次被赋值时产生,使用set命令。
变量可以用unset命令删除,虽然并不强制需要这样做。
变量的值通过$符号访问,也叫变量交换。
Tcl是一个典型的”弱类型定义”语言,这意味者任何类型可以存储在任何变量中。
例如,同一个变量可以存储数字,日期,字符串甚至另一段Tcl script.Example 1.1:set foo "john"puts "Hi my name is $foo"Output: Hi my name is johnExample 1.2:set month 2set day 3set year 97set date "$month:$day:$year"puts $dateOutput: 2:3:97Example 1.3:set foo "puts hi"eval $fooOutput: hi在这个例子里,变量foo存储了另外一段Tcl script.表达式包括数学表达式,关系表达式,通常用expr命令。
Example 2.1:expr 0 == 1Output: 0Example 2.2:expr 1 == 1Output: 1两数比较,true则输出1,false输出0Example 2.3:expr 4 + 5Output: 9Example 2.4:expr sin(2)Output: 0.909297命令传递以运算结果替代Tcl命令中的部分Example 3.1:puts "I am [expr 10 * 2] years old, and my I.Q. is [expr 100 - 25]" Output: I am 20 years old, and my I.Q. is 75方括号是命令传递的标志Example 3.2:set my_height 6.0puts "If I was 2 inches taller, I would be [expr $my_height + (2.0 / 12.0)] feet tall"Output: If I was 2 inches taller, I would be 6.16667 feet tall命令流控制Tcl有判断流转(if-else; switch)和循环控制(while; for; foreach)Example 4.1:set my_planet "earth"if {$my_planet == "earth"} {puts "I feel right at home."} elseif {$my_planet == "venus"} {puts "This is not my home."} else {puts "I am neither from Earth, nor from Venus."}set temp 95if {$temp < 80} {puts "It's a little chilly."} else {puts "Warm enough for me."}Output:I feel right at home.Warm enough for me.Example 4.2:set num_legs 4switch $num_legs {2 {puts "It could be a human."}4 {puts "It could be a cow."}6 {puts "It could be an ant."}8 {puts "It could be a spider."}default {puts "It could be anything."}}Output:It could be a cow.Example 4.3:for {set i 0} {$i < 10} {incr i 1} {puts "In the for loop, and i == $i"}Output:In the for loop, and i == 0In the for loop, and i == 1In the for loop, and i == 2In the for loop, and i == 3In the for loop, and i == 4In the for loop, and i == 5In the for loop, and i == 6In the for loop, and i == 7In the for loop, and i == 8In the for loop, and i == 9Example 4.4:set i 0while {$i < 10} {puts "In the while loop, and i == $i" incr i 1}Output:In the while loop, and i == 0In the while loop, and i == 1In the while loop, and i == 2In the while loop, and i == 3In the while loop, and i == 4In the while loop, and i == 5In the while loop, and i == 6In the while loop, and i == 7In the while loop, and i == 8In the while loop, and i == 9 Example 4.5:foreach vowel {a e i o u} {puts "$vowel is a vowel"}Output:a is a vowele is a voweli is a vowelo is a vowelu is a vowelProceduresTcl的Procedures 和c的函数差不多. 它们有参数,它们返回值。