Tcl语言
上一篇 / 下一篇 2007-07-30 10:25:10 / 个人分类:TCL语言
今天,在无意间看到了“TCL语言”,以前没有听过,也没有看过,上网搜了一下,对它简单的了解学习了一下。需要详细了解请打开下面网址了解,学习。
http://www.tclchina.com/
TCL(Tool Command Language)是一种解释执行的脚本语言(scrīpting Language)。 它提供了通用的编程能力:支持变量、过程和控制结构;同时TCL还拥有一个功能强大的固有的核心命令集。
由于TCL的解释器是用一个C\\C++语言的过程库实现的,因此在某种意义上我们又可以把TCL看作一个C库,这个库中有丰富的用于扩展TCL命令的C\\C++过程和函数,可以很容易就在C\\C++应用程序中嵌入TCL,而且每个应用程序都可以根据自己的需要对TCL语言进行扩展。我们可以针对某一特定应用领域对TCL语言的核心命令集进行扩展,加入适合于自己的应用领域的扩展命令,如果需要,甚至可以加入新的控制结构,TCL解释器将把扩展命令和扩展控制结构与固有命令和固有控制结构同等看待。扩展后的TCL语言将可以继承TCL 核心部分的所有功能,包括核心命令、控制结构、数据类型、对过程的支持等。根据需要,我们甚至可以屏蔽掉TCL的某些固有命令和固有控制结构。通过对TCL的扩展、继承或屏蔽,用户用不着象平时定义一种计算机语言那样对词法、语法、语义、语用等各方面加以定义,就可以方便的为自己的应用领域提供一种功能完备的脚本语言。
TCL良好的可扩展性使得它能很好地适应产品测试的需要,测试任务常常会由于设计和需求的改变而迅速改变,往往让测试人员疲于应付。利用TCL的可扩展性,测试人员就可以迅速继承多种新技术,并针对产品新特点迅速推出扩展TCL命令集,以用于产品的测试中,可以较容易跟上设计需求的变化。 另外,因为TCL是一种比C\\C++ 语言有着更高抽象层次的语言,使用TCL可以在一种更高的层次上编写程序,它屏蔽掉了编写C\\C++程序时必须涉及到的一些较为烦琐的细节,可以大大地提高开发测试例的速度。而且, 使用TCL语言写的测试例脚本,即使作了修改,也用不着重新编译就可以调用TCL解释器直接执行。可以省去不少时间。
Tcl是一个典型的”弱类型定义”语言,这意味者任何类型可以存储在任何变量中。例如,同一个变量可以存储数字,日期,字符串甚至另一段Tcl scrīpt.
Tcl和其他编程语言例如c不同,它是一种解释语言而非编译语言。Tcl程序由一系列Tcl命令组成,在运行时由Tcl解释器解释运行。解释运行的一个优点是它可以自己为自己生成Tcl scrīpt。
变量和变量交换
不像c,Tcl的变量在使用前不需要声明。Tcl的变量在它首次被赋值时产生,使用set命令。变量可以用unset命令删除,虽然并不强制需要这样做。
变量的值通过$符号访问,也叫变量交换。
Tcl是一个典型的”弱类型定义”语言,这意味者任何类型可以存储在任何变量中。例如,同一个变量可以存储数字,日期,字符串甚至另一段Tcl scrīpt.
Example 1.1:
set foo "john"
puts "Hi my name is $foo"
Output: Hi my name is john
Example 1.2:
set month 2
set day 3
set year 97
set date "$month:$day:$year"
puts $date
Output: 2:3:97
Example 1.3:
set foo "puts hi"
eval $foo
Output: hi
在这个例子里,变量foo存储了另外一段Tcl scrīpt.
表达式
包括数学表达式,关系表达式,通常用 expr命令。
Example 2.1:
expr 0 == 1
Output: 0
Example 2.2:
expr 1 == 1
Output: 1
两数比较,true则输出1,false输出0
Example 2.3:
expr 4 + 5
Output: 9
Example 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.0
puts "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 95
if {$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 4
switch $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 == 0
In the for loop, and i == 1
In the for loop, and i == 2
In the for loop, and i == 3
In the for loop, and i == 4
In the for loop, and i == 5
In the for loop, and i == 6
In the for loop, and i == 7
In the for loop, and i == 8
In the for loop, and i == 9
Example 4.4:
set i 0
while {$i < 10} {
puts "In the while loop, and i == $i"
incr i 1
}
Output:
In the while loop, and i == 0
In the while loop, and i == 1
In the while loop, and i == 2
In the while loop, and i == 3
In the while loop, and i == 4
In the while loop, and i == 5
In the while loop, and i == 6
In the while loop, and i == 7
In the while loop, and i == 8
In 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 vowel
e is a vowel
i is a vowel
o is a vowel
u is a vowel
Procedures
Tcl的Procedures 和c的函数差不多. 它们有参数,它们返回值。基本定义方法是:
proc name argList body
当一个procedure被定义,它就被看做是一个命令,如同Tcl的自带命令一样,通过名字来呼叫,名字后面跟上参数。
缺省的,procedure的返回值是它的最后一个命令结果。但也可以通过return命令来返回其他值。Return值可以在procedure的任何地方,一旦执行,procedure就此返回。
Example 5.1:
proc sum_proc {a b} {
return [expr $a + $b]
}
proc magnitude {num} {
if {$num > 0} {
return $num
}
set num [expr $num * (-1)]
return $num
}
set num1 12
set num2 14
set sum [sum_proc $num1 $num2]
puts "The sum is $sum"
puts "The magnitude of 3 is [magnitude 3]"
puts "The magnitude of -2 is [magnitude -2]"
Output:
The sum is 26
The magnitude of 3 is 3
The magnitude of -2 is 2
在procedure中可以通过set创造变量,但是变量只在procedure中有效,而且一旦procedure返回,这些变量就不可访问。如果procedure需要访问主程序中的变量,就需要使用global关键字。
Example 5.2:
proc dumb_proc {} {
set myvar 4
puts "The value of the local variable is $myvar"
global myglobalvar
puts "The value of the global variable is $myglobalvar"
}
set myglobalvar 79
dumb_proc
Output:
The value of the local variable is 4
The value of the global variable is 79
Lists
Lists就好像是Tcl中的一种特殊的数组。它吧一堆东西放成一个集合,然后就像操作一个整体一样的操作它。
Example 6.1:
set simple_list "John Joe Mary Susan"
puts [lindex $simple_list 0]
puts [lindex $simple_list 2]
Output:
John
Mary
注意list的index是从0开始的
Example 6.2:
set simple_list2 "Mike Sam Heather Jennifer"
set compound_list [list $simple_list $simple_list2]
puts $compound_list
puts [llength $compound_list]
Output:
{John Joe Mary Susan} {Mike Sam Heather Jennifer}
2
Example 6.3:
set mylist "Mercury Venus Mars"
puts $mylist
set mylist [linsert $mylist 2 Earth]
puts $mylist
lappend mylist Jupiter
puts $mylist
Output:
Mercury Venus Mars
Mercury Venus Earth Mars
Mercury Venus Earth Mars Jupiter
Arrays
Tcl数组在使用前无须定义,大小也不用指定。
Example 7.1:
set myarray(0) "Zero"
set myarray(1) "One"
set myarray(2) "Two"
for {set i 0} {$i < 3} {incr i 1} {
puts $myarray($i)
}
Output:
Zero
One
Two
Example 7.2:
set person_info(name) "Fred Smith"
set person_info(age) "25"
set person_info(occupation) "Plumber"
foreach thing {name age occupation} {
puts "$thing == $person_info($thing)"
}
Output:
name == Fred Smith
age == 25
occupation == Plumber
这个例子指出数组的index不需要是数字,其他类型的数据也可以。
Example 7.3:
set person_info(name) "Fred Smith"
set person_info(age) "25"
set person_info(occupation) "Plumber"
foreach thing [array names person_info] {
puts "$thing == $person_info($thing)"
}
Output:
occupation == Plumber
age == 25
name == Fred Smith
Strings
字符串是Tcl中最常用的类型,string有很多使用参数,可以参照Tcl手册。使用方法:
string option arg arg ...
Example 8.1:
set str "This is a string"
puts "The string is: $str"
puts "The length of the string is: [string length $str]"
puts "The character at index 3 is: [string index $str 3]"
puts "The characters from index 4 through 8 are: [string range $str 4 8]"
puts "The index of the first occurrence of letter \"i\" is: [string first i $str]"
Output:
The string is: This is a string
The length of the string is: 16
The character at index 3 is: s
The characters from index 4 through 8 are: is a
The index of the first occurrence of letter "i" is: 2
Input/Output
Tcl的绝大多数输入/输出是通过puts和gets做到的。Puts命令显示在console上,gets命令从console输入上取得数据,并存储在某个变量中。
gets channelId varName
channelID可以理解为c的文件句柄,varName如果定义,输入值就赋给它,gets返回读入的字节数,否则gets直接返回输入值。
Example 9.1:
puts -nonewline "Enter your name: "
set bytesread [gets stdin name]
puts "Your name is $name, and it is $bytesread bytes long"
Output: (note that user input is shown in italics)
Enter your name: Shyam
Your name is Shyam, and it is 5 bytes long
Example 9.2:
set f [open "/tmp/myfile" "w"]
puts $f "We live in Texas. It's already 110 degrees out here."
puts $f "456"
close $f
Output: (none)
Open打开了一个 "/tmp/myfile" 作为channel. 用法是:
open name access
access参数指出打开文件的方式,”w”是读写。这时可以用puts $f把内容写入文件
Example 9.3:
set f [open "/tmp/myfile" "r"]
set line1 [gets $f]
set len_line2 [gets $f line2]
close $f
puts "line 1: $line1"
puts "line 2: $line2"
puts "Length of line 2: $len_line2"
Output:
line 1: We live in Texas. It's already 110 degrees out here.
line 2: 456
Length of line 2: 3
这个例子假设已知文件只有两行,如果不是,则需要使用循环,用eof来找到文件尾。
eval
eval命令会把它的参数直接送往解释器。
Example 10.1:
set foo "set a 22"
eval $foo
puts $a
Output:
22
单纯的执行$foo不会成功。
catch
Example 10.2:
set retval [catch {set f [open "nosuchfile" "r"]}]
if {$retval == 1} {
puts "An error occured"
}
Output: (this output occurs if there is no file named "nosuchfile" in the current directory).
An error occured
Catch 参数记录一个scrīpt的执行情况,如果返回值为1,则执行出错。用来进行错误处理。
·······································································
TAG: TCL语言
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | ||||||
5 | 6 | 7 | 8 | 9 | 10 | 11 | |||
12 | 13 | 14 | 15 | 16 | 17 | 18 | |||
19 | 20 | 21 | 22 | 23 | 24 | 25 | |||
26 | 27 | 28 | 29 | 30 | 31 |
我的存档
数据统计
- 访问量: 77567
- 日志数: 101
- 图片数: 2
- 文件数: 1
- 书签数: 2
- 建立时间: 2007-03-02
- 更新时间: 2009-04-15