UG后处理tcl语言解释.docx

上传人:b****4 文档编号:3913991 上传时间:2023-05-06 格式:DOCX 页数:15 大小:19.96KB
下载 相关 举报
UG后处理tcl语言解释.docx_第1页
第1页 / 共15页
UG后处理tcl语言解释.docx_第2页
第2页 / 共15页
UG后处理tcl语言解释.docx_第3页
第3页 / 共15页
UG后处理tcl语言解释.docx_第4页
第4页 / 共15页
UG后处理tcl语言解释.docx_第5页
第5页 / 共15页
UG后处理tcl语言解释.docx_第6页
第6页 / 共15页
UG后处理tcl语言解释.docx_第7页
第7页 / 共15页
UG后处理tcl语言解释.docx_第8页
第8页 / 共15页
UG后处理tcl语言解释.docx_第9页
第9页 / 共15页
UG后处理tcl语言解释.docx_第10页
第10页 / 共15页
UG后处理tcl语言解释.docx_第11页
第11页 / 共15页
UG后处理tcl语言解释.docx_第12页
第12页 / 共15页
UG后处理tcl语言解释.docx_第13页
第13页 / 共15页
UG后处理tcl语言解释.docx_第14页
第14页 / 共15页
UG后处理tcl语言解释.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

UG后处理tcl语言解释.docx

《UG后处理tcl语言解释.docx》由会员分享,可在线阅读,更多相关《UG后处理tcl语言解释.docx(15页珍藏版)》请在冰点文库上搜索。

UG后处理tcl语言解释.docx

UG后处理tcl语言解释

TCL语言

Tcl(最早称为“工具命令语言”"ToolCommandLanguage",但是目前已经不是这个含义,不过我们仍然称呼它为TCL)是一种脚本语言。

由JohnOusterhout创建。

TCL很好学,功能很强大。

TCL经常被用于快速原型开发,脚本编程,GUI和测试等方面。

  Tcl(最早称为“工具命令语言”"ToolCommandLanguage",但是目前已经不是这个含义,不过我们仍然称呼它为TCL)是一种脚本语言。

由JohnOusterhout创建。

TCL很好学,功能很强大。

TCL经常被用于快速原型开发,脚本编程,GUI和测试等方面。

TCL念作“踢叩”"tickle".Tcl的特性包括:

  *任何东西都是一条命令,包括语法结构(for,if等)。

  *任何事物都可以重新定义和重载。

  *所有的数据类型都可以看作字符串。

  *语法规则相当简单

  *提供事件驱动给Socket和文件。

基于时间或者用户定义的事件也可以。

  *动态的域定义。

  *很容易用C,C++,或者Java扩展。

  *解释语言,代码能够动态的改变。

  *完全的Unicode支持。

  *平台无关。

Win32,UNIX,Mac上都可以跑。

  *和Windows的GUI紧密集成。

Tk

  *代码紧凑,易于维护。

  TCL本身不提供面向对象的支持。

但是语言本身很容易扩展到支持面向对象。

许多C语言扩展都提供面向对象能力,包括XOTcl,IncrTcl等。

另外SNIT扩展本身就是用TCL写的。

  使用最广泛的TCL扩展是TK。

TK提供了各种OS平台下的图形用户界面GUI。

连强大的Python语言都不单独提供自己的GUI,而是提供接口适配到TK上。

另一个流行的扩展包是Expect.Expect提供了通过终端自动执行命令的能力,例如(passwd,ftp,telnet等命令驱动的外壳).

具体例子

  下面是TCL程序的例子:

  #!

/bin/sh

  #nextlinerestartsusingtclshinpath\

  exectclsh${1+"$@"}

  #echoserverthatcanhandlemultiple

  #simultaneousconnections.

  procnewConnection{sockaddrport}{

  #clientconnectionswillbehandledin

  #line-buffered,non-blockingmode

  fconfigure$sock-blockingno-bufferingline

  #callhandleDatawhensocketisreadable

  fileevent$sockreadable[listhandleData$sock]

  }

  prochandleData{

  puts$sock[gets$sock]

  if{[eof$sock]}{

  close$sock

  }

  }

  #handleallconnectionstoportgiven

  #asargumentwhenserverwasinvoked

  #bycallingnewConnection

  setport[lindex$argv0]

  socket-servernewConnection$port

  #entertheeventloopbywaiting

  #onadummyvariablethatisotherwise

  #unused.

  vwaitforever

  另外一个TK的例子(来自AsimpleA/Dclock)它使用了定时器时间,3行就显示了一个时钟。

  procevery{msbody}{eval$body;after$ms[infolevel0]}

  pack[label.clock-textvartime]

  every1000{set:

:

time[clockformat[clocksec]-format%H:

%M:

%S]};#RS

  解释:

第一行定义了过程every,每隔ms毫秒,就重新执行body代码。

第二行创建了标签起内容由time变量决定。

第3行中设置定时器,time变量从当前时间中每秒更新一次。

  Tcl被广泛的用做script语言,大多数情况下,Tcl和Tk(“ToolKit”)库同时使用,Tk是一系列令Tcl易于编写图形用户接口的命令和过程

  Tcl的一个重要特性是它的扩展性。

如果一个程序需要使用某些标准Tcl没有提供的功能,可以使用c语言创造一些新的Tcl命令,并很容易的融合进去。

正是由于Tcl易于扩展,很多人为它编写了扩展包,并在网上共享。

  Tcl和其他编程语言例如c不同,它是一种解释语言而非编译语言。

Tcl程序由一系列Tcl命令组成,在运行时由Tcl解释器解释运行。

解释运行的一个优点是它可以自己为自己生成Tclscript。

变量和变量交换

  不像c,Tcl的变量在使用前不需要声明。

Tcl的变量在它首次被赋值时产生,使用set命令。

变量可以用unset命令删除,虽然并不强制需要这样做。

  变量的值通过$符号访问,也叫变量交换。

  Tcl是一个典型的”弱类型定义”语言,这意味者任何类型可以存储在任何变量中。

例如,同一个变量可以存储数字,日期,字符串甚至另一段Tclscript.

  Example1.1:

  setfoo"john"

  puts"Himynameis$foo"

  Output:

Himynameisjohn

  Example1.2:

  setmonth2

  setday3

  setyear97

  setdate"$month:

$day:

$year"

  puts$date

  Output:

2:

3:

97

  Example1.3:

  setfoo"putshi"

  eval$foo

  Output:

hi

  在这个例子里,变量foo存储了另外一段Tclscript.

  表达式

  包括数学表达式,关系表达式,通常用expr命令。

  Example2.1:

  expr0==1

  Output:

0

  Example2.2:

  expr1==1

  Output:

1

  两数比较,true则输出1,false输出0

  Example2.3:

  expr4+5

  Output:

9

  Example2.4:

  exprsin

(2)

  Output:

0.909297

  命令传递

  以运算结果替代Tcl命令中的部分

  Example3.1:

  puts"Iam[expr10*2]yearsold,andmyI.Q.is[expr100-25]"

  Output:

Iam20yearsold,andmyI.Q.is75

  方括号是命令传递的标志

  Example3.2:

  setmy_height6.0

  puts"IfIwas2inchestaller,Iwouldbe[expr$my_height+(2.0/12.0)]feettall"

  Output:

IfIwas2inchestaller,Iwouldbe6.16667feettall

  命令流控制

  Tcl有判断流转(if-else;switch)和循环控制(while;for;foreach)

  Example4.1:

  setmy_planet"earth"

  if{$my_planet=="earth"}{

  puts"Ifeelrightathome."

  }elseif{$my_planet=="venus"}{

  puts"Thisisnotmyhome."

  }else{

  puts"IamneitherfromEarth,norfromVenus."

  }

  settemp95

  if{$temp<80}{

  puts"It'salittlechilly."

  }else{

  puts"Warmenoughforme."

  }

  Output:

  Ifeelrightathome.

  Warmenoughforme.

  Example4.2:

  setnum_legs4

  switch$num_legs{

  2{puts"Itcouldbeahuman."}

  4{puts"Itcouldbeacow."}

  6{puts"Itcouldbeanant."}

  8{puts"Itcouldbeaspider."}

  default{puts"Itcouldbeanything."}

  }

  Output:

  Itcouldbeacow.

  Example4.3:

  for{seti0}{$i<10}{incri1}{

  puts"Intheforloop,andi==$i"

  }

  Output:

  Intheforloop,andi==0

  Intheforloop,andi==1

  Intheforloop,andi==2

  Intheforloop,andi==3

  Intheforloop,andi==4

  Intheforloop,andi==5

  Intheforloop,andi==6

  Intheforloop,andi==7

  Intheforloop,andi==8

  Intheforloop,andi==9

  Example4.4:

  seti0

  while{$i<10}{

  puts"Inthewhileloop,andi==$i"

  incri1

  }

  Output:

  Inthewhileloop,andi==0

  Inthewhileloop,andi==1

  Inthewhileloop,andi==2

  Inthewhileloop,andi==3

  Inthewhileloop,andi==4

  Inthewhileloop,andi==5

  Inthewhileloop,andi==6

  Inthewhileloop,andi==7

  Inthewhileloop,andi==8

  Inthewhileloop,andi==9

  Example4.5:

  foreachvowel{aeiou}{

  puts"$vowelisavowel"

  }

  Output:

  aisavowel

  eisavowel

  iisavowel

  oisavowel

  uisavowel

  Procedures

参数定义方法

  Tcl的Procedures和c的函数差不多.它们有参数,它们返回值。

基本定义方法是:

  procnameargListbody

  当一个procedure被定义,它就被看做是一个命令,如同Tcl的自带命令一样,通过名字来呼叫,名字后面跟上参数。

  缺省的,procedure的返回值是它的最后一个命令结果。

但也可以通过return命令来返回其他值。

Return值可以在procedure的任何地方,一旦执行,procedure就此返回。

  Example5.1:

  procsum_proc{ab}{

  return[expr$a+$b]

  }

  procmagnitude{num}{

  if{$num>0}{

  return$num

  }

  setnum[expr$num*(-1)]

  return$num

  }

  setnum112

  setnum214

  setsum[sum_proc$num1$num2]

  puts"Thesumis$sum"

  puts"Themagnitudeof3is[magnitude3]"

  puts"Themagnitudeof-2is[magnitude-2]"

  Output:

  Thesumis26

  Themagnitudeof3is3

  Themagnitudeof-2is2

  在procedure中可以通过set创造变量,但是变量只在procedure中有效,而且一旦procedure返回,这些变量就不可访问。

如果procedure需要访问主程序中的变量,就需要使用global关键字。

  Example5.2:

  procdumb_proc{}{

  setmyvar4

  puts"Thevalueofthelocalvariableis$myvar"

  globalmyglobalvar

  puts"Thevalueoftheglobalvariableis$myglobalvar"

  }

  setmyglobalvar79

  dumb_proc

  Output:

  Thevalueofthelocalvariableis4

  Thevalueoftheglobalvariableis79

List

  Lists就好像是Tcl中的一种特殊的数组。

它把一堆东西放成一个集合,然后就像操作一个整体一样的操作它。

  Example6.1:

  setsimple_list"JohnJoeMarySusan"

  puts[lindex$simple_list0]

  puts[lindex$simple_list2]

  Output:

  John

  Mary

  注意list的index是从0开始的

  Example6.2:

  setsimple_list2"MikeSamHeatherJennifer"

  setcompound_list[list$simple_list$simple_list2]

  puts$compound_list

  puts[llength$compound_list]

  Output:

  {JohnJoeMarySusan}{MikeSamHeatherJennifer}

  2

  Example6.3:

  setmylist"MercuryVenusMars"

  puts$mylist

  setmylist[linsert$mylist2Earth]

  puts$mylist

  lappendmylistJupiter

  puts$mylist

  Output:

  MercuryVenusMars

  MercuryVenusEarthMars

  MercuryVenusEarthMarsJupiter

  Arrays

  Tcl数组在使用前无须定义,大小也不用指定。

  Example7.1:

  setmyarray(0)"Zero"

  setmyarray

(1)"One"

  setmyarray

(2)"Two"

  for{seti0}{$i<3}{incri1}{

  puts$myarray($i)

  }

  Output:

  Zero

  One

  Two

  Example7.2:

  setperson_info(name)"FredSmith"

  setperson_info(age)"25"

  setperson_info(occupation)"Plumber"

  foreachthing{nameageoccupation}{

  puts"$thing==$person_info($thing)"

  }

  Output:

  name==FredSmith

  age==25

  occupation==Plumber

  这个例子指出数组的index不需要是数字,其他类型的数据也可以。

  Example7.3:

  setperson_info(name)"FredSmith"

  setperson_info(age)"25"

  setperson_info(occupation)"Plumber"

  foreachthing[arraynamesperson_info]{

  puts"$thing==$person_info($thing)"

  }

  Output:

  occupation==Plumber

  age==25

  name==FredSmith

  Strings

  字符串是Tcl中最常用的类型,string有很多使用参数,可以参照Tcl手册。

使用方法:

  stringoptionargarg...

  Example8.1:

  setstr"Thisisastring"

  puts"Thestringis:

$str"

  puts"Thelengthofthestringis:

[stringlength$str]"

  puts"Thecharacteratindex3is:

[stringindex$str3]"

  puts"Thecharactersfromindex4through8are:

[stringrange$str48]"

  puts"Theindexofthefirstoccurrenceofletter\"i\"is:

[stringfirsti$str]"

  Output:

  Thestringis:

Thisisastring

  Thelengthofthestringis:

16

  Thecharacteratindex3is:

s

  Thecharactersfromindex4through8are:

isa

  Theindexofthefirstoccurrenceofletter"i"is:

2

  Input/Output

  Tcl的绝大多数输入/输出是通过puts和gets做到的。

Puts命令显示在console上,gets命令从console输入上取得数据,并存储在某个变量中。

getschannelIdvarName

  channelID可以理解为c的文件句柄,varName如果定义,输入值就赋给它,gets返回读入的字节数,否则gets直接返回输入值。

  Example9.1:

  puts-nonewline"Enteryourname:

"

  setbytesread[getsstdinname]

  puts"Yournameis$name,anditis$bytesreadbyteslong"

  Output:

(notethatuserinputisshowninitalics)

  Enteryourname:

Shyam

  YournameisShyam,anditis5byteslong

  Example9.2:

  setf[open"/tmp/myfile""w"]

  puts$f"WeliveinTexas.It'salready110degreesouthere."

  puts$f"456"

  close$f

  Output:

(none)

  Open打开了一个"/tmp/myfile"作为channel.用法是:

  opennameaccess

  access参数指出打开文件的方式,”w”是读写。

这时可以用puts$f把内容写入文件

  Example9.3:

  setf[open"/tmp/myfile""r"]

  setline1[gets$f]

  setlen_line2[gets$fline2]

  close$f

  puts"line1:

$line1"

  puts"line2:

$line2"

  puts"Lengthofline2:

$len_line2"

  Output:

  line1:

WeliveinTexas.It'salready110degreesouthere.

  line2:

456

  Lengthofline

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2