`
universsky
  • 浏览: 99365 次
文章分类
社区版块
存档分类
最新评论

脚本语言TCl教程:6

 
阅读更多

source

1. source的用途

Ø 将一个程序分为多个文件;

Ø 可以将一组过程放到一个文件中,成为一个库文件;

Ø 配置程序;

Ø 加载数据文件。

2. 格式:source fileName

说明:

Ø 读入文件并执行;

Ø 如果代码出错,source返回那个错误

Ø 如果执行到返回,就立刻返回,即便返回命令后面还有命令也不执行立刻返回

Ø 如果文件名以 ~ 开头,替换为环境变量 $HOME

例子:031_source.tcl

set filename "C:\\windows\\temp\\TT_[pid]"

set outfile [open "$filename" "w"];

puts $outfile {set scr [info script]}

puts $outfile "proc testproc {} {"

puts $outfile "global scr;"

puts $outfile "puts \"testproc source file: \$scr.\""

puts $outfile "puts \"testproc executing from \[info script]\n\""

puts $outfile "}"

puts $outfile {set abc 1};

puts $outfile {return};

puts $outfile {set aaaa 1} ;#没有执行这一句

close $outfile;

puts "This is the contents of $filename:"

puts ".............................................................."

puts "[exec cmd /C type $filename]"

puts ".............................................................."

puts "\n"

puts "Global variables visible before sourceing $filename:"

puts "[lsort [info globals]]\n"

if {[info procs testproc] == ""} {

puts "testproc does not exist. sourceing $filename"

source $filename ;#加载上过程后,就可以调用了

}

puts "\nNow executing testproc"

testproc ;#执行过程

puts "Global variables visible after sourceing $filename:"

puts "[lsort [info globals]]\n"

exec cmd /c del $filename ;#删除该文件

1.2 :建库-unknown & info library

1. 自动加载库文件的方法:

2. 建库的相关函数列表

序号

函数

描述

1

auto_mkindex libdir file1...filen

为库文件创建索引文件(tclIndex),自动加载库的过程定义在init.tcl 文件中,当tclsh启动的时候执行

2

info library

返回库所在路径,实际是环境变量TCL_LIBRARY的值,tcl会在该路径下查找init.tcl

3

unknown args

当解释器遇到调用索引文件中不存在的过程是,该函数尝试下面的步骤来执行命令:

1. 查找auto_path的路径中的索引文件,如果找到过程定义,使用auto_load过程加载执行;

2. 如果解释器交互执行的,tcl尝试使用过程auto_exec去执行;

3. 如果命令是个不带后缀名的tcl命令,unknown完善命令名字,并执行

例子:032_source.tcl

;# Set up a temporary file with a test proc.

set filename "C:/temp/TT_[pid]" ;#1. 生成文件

set outfile [open "$filename" "w"];

puts $outfile {set scr [info script]}

puts $outfile "proc testproc {} {"

puts $outfile "global scr;"

puts $outfile "puts \"testproc source file: \$scr.\""

puts $outfile "puts \"testproc executing from \[info script]\n\"" ;#注意:返回的是032_source.tcl 而不是TT_[pid]

puts $outfile "}"

close $outfile;

puts "The directories in the auto path are: $auto_path\n"

puts "The default library is: [info library]\n";

auto_mkindex "C:/temp" [file tail $filename] ;#2. 对文件建立索引文件,c:/temp 路径下生成索引文件 tclIndex

# With Tcl8.3, this must come after auto_mkindex.

lappend auto_path "C:/temp" ;#3. 将库的路径加到 auto_path

if { [info procs testproc] == ""} {

puts "testproc does not exist\n"

}

testproc

if { [info procs testproc] != ""} {

puts "\ntestproc does exist now"

}

file delete C:/temp/tclIndex

file delete $filename

1.3 :创建命令-eval

1.在程序运行的过程中,tcl可以执行其中创建的命令

格式:eval arg1 ??arg2??... ??argn??

功能:将参数连接成一个字符串,传递给tcl_Eval 进行执行,并返回结果或错误码

例子:033_eval.tcl

set cmd {puts "Evaluating a puts"}

puts "CMD IS: $cmd"

eval $cmd

if {[string match [info procs tempFileName] ""] } {

puts "\nDefining tempFileName for this invocation"

set num 0;

set cmd "proc tempFileName "

set cmd [concat $cmd "{} {\n"]

set cmd [concat $cmd "global num;\n"]

set cmd [concat $cmd "incr num;\n"]

set cmd [concat $cmd " return \"/tmp/TMP.[pid].\$num\";\n"]

set cmd [concat $cmd "}"]

eval $cmd

}

puts "\nThe body of tempFileName is: \n[info body tempFileName]\n"

puts "tmpFileName returns: [tempFileName]" ;# 这里$num返回1

puts "tmpFileName returns: [tempFileName]" ;# $num加一,返回2

1.4 :在eval中应用format & list

1.常见的eval错误

okeval puts ok

erreval puts not ok

okset x “OK” ; eval puts $x

errset x “NOT OK”; eval puts $x

not ok被解析为了两个参数

下面三种写法是正确的:

eval [list puts {NOT OK}]

eval [list puts “NOT OK”]

set cmd “puts”; lappend cmd {NOT OK};eval $cmd

结论:对于构建eval的需要的命令,要么使用stringformat,要么使用listlappend

2.格式:info complete string

功能:检查字符串中的空格,引号,括号等是否匹配,匹配返回一,不匹配返回零

例子:034_format_list.tcl

set cmd "OK"

eval puts $cmd ;#正确

set cmd "puts" ; lappend cmd {Also OK}; eval $cmd ;#正确

set cmd "NOT OK"

#eval puts $cmd ;#出错

eval [format {%s "%s"} puts "Even This Works"] ;#正确

set cmd "And even this can be made to work"

eval [format {%s "%s"} puts $cmd ] ;#正确

set tmpFileNum 0;

set cmd {proc tempFileName }

lappend cmd ""

lappend cmd "global num; incr num; return \"/tmp/TMP.[pid].\$tmpFileNum\"" ;#这里$tmpFileNum不会返回0,会返回$tmpFileNum,因为它是在过程外赋值的

eval $cmd

puts "\nThis is the body of the proc definition:"

puts "[info body tempFileName]\n"

set cmd {puts "This is Cool!"}

if {[info complete $cmd]} {

eval $cmd

} else {

puts "INCOMPLETE COMMAND: $cmd"

}

1.5 :不使用eval替换-format & subst

1.不使用eval的时候,字符串会进行一次置换

例如: set a “sampleA”

set c a

puts “$$c” ;#会返回$a,而不是$a的值

要想进行多次置换,需要使用format或者subst,使用一次置换一次

注意:使用双引号会置换一次,但是使用花括号不会置换

2.格式:subst ?-nobackslashes??-nocommands??-novariables? string

-no后面接什么就不置换什么

例子:

set a "alpha"

set b a

puts {a and b with no substitution: $a $$b}

puts "a and b with one pass of substitution: $a $$b"

puts "a and b with subst in braces: [subst {$a $$b}]"

puts "a and b with subst in quotes: [subst "$a $$b"]\n"

puts "format with no subst [format {$%s} $b]"

puts "format with subst: [subst [format {$%s} $b]]"

eval "puts \"eval after format: [format {$%s} $b]\""

set num 0;

set cmd "proc tempFileName {} "

set cmd [format "%s {global num; incr num;" $cmd]

set cmd [format {%s return "/tmp/TMP.%s.$num"} $cmd [pid] ]

set cmd [format "%s }" $cmd ]

eval $cmd

puts "[info body tempFileName]"

set a arrayname

set b index

set c newvalue

eval [format "set %s(%s) %s" $a $b $c]

puts "Index: $b of $a was set to: $arrayname(index)"

set x "xyz"

set y x

set z y

puts "xyz is : $x $$y $$$z"

puts "xyz is : [subst "$x $$y $$$z"]" ;#返回:xyz xyz $x

puts "xyz is : [subst [subst "$x $$y $$$z"]]" ;#返回:xyz xyz xyz

1.6 :改变工作目录- cd & pwd

1. 改变工作目录

格式:cd ?dirName?

功能:改变当前目录到dirName,如果没有dirName就改变到当前用户的工作目录$HOME,或者目录是 ~ ,也是改变到当前用户的工作目录$HOME,如果是 ~ 开头,后面紧跟的字符被解析为loginid

2. 显示当前路径:pwd

例子:036_cd_pwd.tcl

set dirs [list C:/windows C:/windows/system C:/temp C:/foo ]

puts "[format "%-15s %-20s " "FILE" "DIRECTORY"]"

foreach dir $dirs {

catch {cd $dir}

set c_files [glob -nocomplain c*]

foreach name $c_files {

puts "[format "%-15s %-20s " $name [pwd]]"

}

}

1.7 :调试和错误-errorinfo & errorCode & catch

1.错误相关:

序号

命令或变量

描述

1

error message ?info? ?code?

生成一个错误条件,如果info或者code有值,变量errorInfoerrorCode就这两个值被初始化

2

catch script ?varName?

执行script,如果成功返回TCL_OK,否则返回TCL_ERROR,结果存在varName中。书上这么说的,但是我的试验结果是失败varName存储的错误信息,成功varName存储的0

3

return ?-code code? ?-errorinfo info??-errorcode errorcode??value?

生成一个返回异常条件

-code :返回状态,可以是:

okerror,无,break

-errorinfoinfoerrorInfo变量中的第一个字符串

-errorcode:过程设置errorcode为全局变量的errorCode

value:过程返回值

4

errorInfo

包含了命令错误信息的全局变量

5

errorCode

包含了命令错误代码的全局变量

例子:037_error.tcl

proc errorproc {x} {

if {$x > 0} {

error "Error generated by error" "Info String for error" $x

}

}

catch errorproc

puts "after bad proc call: ErrorCode: $errorCode" ;#这里errorCode返回NONE

puts "ERRORINFO:\n$errorInfo\n"

set errorInfo "";

catch {errorproc 0}

puts "after proc call with no error: ErrorCode: $errorCode" ;#这里errorCode返回NONE,没有错误提示信息

puts "ERRORINFO:\n$errorInfo\n"

catch {errorproc 2}

puts "after error generated in proc: ErrorCode: $errorCode" ;#这里errorCode返回2

puts "ERRORINFO:\n$errorInfo\n"

proc returnErr { x } {

return -code error -errorinfo "Return Generates This" -errorcode "-999"

}

catch {returnErr 2}

puts "after proc that uses return to generate an error: ErrorCode: $errorCode" ;#这里errorCode返回999

1.8 puts "ERRORINFO:\n$errorInfo\n" ;#这里返回的错误信息:调试-trace

1.调试方法:

序号

命令

描述

1

trace variable variableName operation procname

跟踪变量variableName

operation 可以是:

rread ,读

wwrite ,写

uunset ,取消

当变量有相应操作执行procname

2

trace vdelete variableName operation procname

取消变量variableName相应操作operation的跟踪

3

trace vinfo variableName

返回跟踪的变量variableName的信息

例子:

proc traceproc {variableName arrayElement operation} {

set op(w) "Write"; set op(u) "Unset"; set op(r) "Read" ;#定义一个操作数组

set level [info level]

incr level -1;

if {$level > 0} {

set procid [info level $level]

} else {set procid "main"} ;#当级别为零,设置为main,在主程序中

if {![string match $arrayElement ""]} { ;#判断跟踪的变量是否是数组

puts "TRACE: $op($operation) $variableName($arrayElement) in $procid"

} else {

puts "TRACE: $op($operation) $variableName in $procid"

}

}

proc testProc {input1 input2} {

upvar $input1 i

upvar $input2 j

set i 2

set k $j;

}

1.9 trace variable i1 w traceproc ;#传递给traceproc的三个参数分别是:i1,空,w,当i1是数组的时候,:命令行参数和环境串

1.程序和用户交互的方法有很多,以下是其中四种:

Ø 提示用户输入需要信息;

Ø 读入配置文件;

Ø 从命令行读入参数;

Ø 从环境变量读入

2.命令行参数:argc得到参数个数,argv0得到程序自身名字,argv得到其他参数,注意没有argv1这类的全局变量

3env是存储着环境变量的数组,对环境变量修改只是当前程序中生效了

例子:039_arg.tcl

puts "There are $argc arguments to this script"

puts "The name of this script is $argv0" ;#得到程序名

if {$argc > 0} {puts "The other arguments are: $argv" } ;#得到其他的参数

puts "You have these environment variables set:"

foreach index [array names env] {

puts "$index: $env($index)"

}

set prompt "\$P"

if { [info exists env(PROMPT)] } {

puts "$env(PROMPT)" ;#返回 $P$G

set env(PROMPT) $prompt ;#设置环境变量为$P,程序执行完后仍然是$P$G,因为它只是在该程序中生效了

puts "$env(PROMPT)"

}

分享到:
评论

相关推荐

    TCL脚本语言详细教程

    ### TCL脚本语言详细教程 #### 一、TCL语法概览 TCL(Tool Command Language)是一种轻量级的脚本语言,以其简洁高效的特点广泛应用于自动化测试、配置管理等多个领域。对于初学者而言,了解TCL的基本语法是至关...

    TCL+TCL脚本语言编程+TCL语言教程

    以下是对TCL脚本语言编程和TCL语言教程的一些关键知识点的详细说明: 1. **基本语法**:TCL的语法简洁明了,以空格和换行符作为语句分隔。命令通常由关键字和参数组成,不区分大小写。例如,`puts`命令用于打印输出...

    tcl脚本语言的教程

    TCL(Tool Command Language)是一种解释型的、动态类型的脚本语言,由John Ousterhout教授在1988年开发。它以其简洁、易学的特点被广泛应用于各种领域,包括系统管理、网络编程、GUI开发等。下面将详细介绍TCL脚本...

    tcl中文教程---最好的Tcl脚本语言的中文教程,值得下载

    本文将根据提供的《TCL中文教程》的部分内容,总结并详细介绍TCL脚本语言的关键知识点。 #### 二、TCL语法 TCL的语法简洁而强大,主要包括脚本结构、单词和命令等组成部分。 ##### 2.1 脚本、命令和单词符号 - **...

    脚本语言Tcl教程.pdf

    TCL(Tool Command Language)作为一门功能强大且灵活的脚本语言,在众多领域有着广泛应用。这本教程以其独特的魅力,成为学习 TCL 语言的不二之选。 教程内容全面且系统,从 TCL 语言的基础语法讲起,细致入微地...

    TCL.zip_TCL教程_tcL学习_tcl脚本_tcl语言_脚本 tcl

    TCL(Tool Command Language)是一种强大的脚本语言,由John Ousterhout在1988年开发。它被设计为一种简洁、易学、易用的语言,主要用于系统管理、网络编程、GUI(图形用户界面)开发以及嵌入式系统的控制。TCL以其...

    TCL脚本语言培训教程(中文版)

    ### TCL脚本语言培训教程(中文版)知识点详解 #### 1. 引言 - **TCL**(Tool Command Language)是一种轻量级的解释型脚本语言,以其简洁性和易用性著称。 - **适用场景**:主要用于自动化测试、系统管理任务以及...

    TCL教程(中文).pdf

    TCL是一种轻量级的脚本语言,广泛用于自动化任务、系统管理以及嵌入式系统。这份TCL教程中文版提供了全面的学习资源,涵盖了从基础语法到高级特性的方方面面。 ### TCL语法 TCL的语法相对简洁,由脚本、命令和单词...

    TCL脚本语言基本教程

    **TCL脚本语言基本教程** TCL(Tool Command Language),一种强大且灵活的脚本语言,由John Ousterhout在20世纪80年代末创建,最初用于编写工具命令。TCL以其简单易学、语法简洁以及与其他语言的无缝集成而闻名,...

    TCL脚本语言入门教程

    TCL脚本语言入门教程 TCL(Tool Command Language)是一种高级的脚本语言,广泛应用于自动化测试、网络管理、数据库管理等领域。下面是TCL脚本语言入门教程的详细知识点总结。 一、TCL语法 TCL语法是TCL语言的...

    系统学习TCL脚本入门教程

    它最初设计用于快速开发简单的命令行工具,但随着其发展,TCL已经成为一种功能全面的脚本语言,能够处理复杂的任务,甚至可以与C++或Java等编译型语言进行集成。 运行TCL脚本需要一个TCL解释器,如ActiveTCL,它可...

    TCL脚本语言编程

    ### TCL脚本语言编程知识点详解 #### 一、TCL基本概念 **TCL全称:** Tool Command Language,是一种轻量级的脚本语言,主要用于快速开发各种自动化任务、配置管理和GUI应用。 **特点:** - **基于字符串:** 大...

    TCL脚本语言手册(很详细)

    ### TCL脚本语言手册知识点概览 #### 一、TCL基本知识 **1.1 什么是TCL** TCL(Tool Command Language)是一种轻量级的解释型脚本语言,设计初衷是为了方便用户快速地编写小型应用程序或脚本来解决各种问题。TCL...

    Tcl语言全攻略,TCL脚本语言

    Tcl(Tool Command Language,工具命令语言)是一种简单、易学、强大的脚本语言,尤其适合于快速开发和集成系统。在华为等大型科技公司中,Tcl因其灵活性和高效性,常被用于自动化测试、网络设备配置、系统管理等...

    TCL脚本语言编程.pdf

    描述与标签中的重复提及“TCL脚本语言编程.pdf”,暗示了这份文档可能是关于TCL编程的详细介绍或教程,旨在帮助读者掌握TCL语言的基础知识、高级特性以及实际应用案例。由于文档的具体内容未能完全展示,我们可以...

    Tcl教程资料.zip

    Tcl(Tool Command Language)是一种动态类型的脚本语言,由John Ousterhout在1988年开发,主要用于编写系统管理和应用控制脚本。它以其简洁、易学和强大的字符串处理能力而闻名。Tcl_Tk是Tcl语言的一个扩展,提供了...

    TCL 语言学习教程

    TCL(Tool Command Language)是一种高级的脚本语言,广泛应用于自动化、测试和开发领域。下面是TCL语言的学习指南,涵盖了TCL语言的基础知识、语法、特性和应用场景。 TCL语言基础 * TCL是一种解释型语言,支持多...

    Tcl详细教程(ppt)

    Tcl是一种简洁而强大的脚本语言,主要用于自动化任务、软件集成和GUI开发。Tcl/Tk教程详细地讲解了Tcl语言的语法和核心概念,同时也涵盖了面向对象编程的特性,使得学习者能够全面理解并掌握Tcl。 1. Tcl语法: ...

    tcl脚本教程入门书

    TCL,全称为Tool Command Language或Terminal Control Language,是一种解释型的脚本语言,其特点是简洁易学且功能强大。TCL的解释器是`tclsh`,它同时也是一种shell环境。由于TCL与C语言在数据类型和控制结构上有...

    系统学习TCL脚本入门教程.doc

    TCL(Tool Command Language)是一种广泛应用于嵌入式系统、网络设备和自动化测试等领域的脚本语言。本教程旨在为读者提供一个系统的TCL脚本入门学习指南,帮助读者快速掌握TCL脚本的基础知识和编程技术。 1. TCL...

Global site tag (gtag.js) - Google Analytics