TCL学习之info命令

这篇文章主要讲一下info指令的相关用法。

1.info命令列表

该命令使用的匹配式规则和string match 一致,并且如果不适用匹配式,返回所有的项。

序号命令描述
1info commands ?pattern?返回匹配的命令列表
2info exists varName变量存在返回1,否则返回0
3info globals?pattern?返回全局变量列表
4info locals? pattern?返回局部变量列表
5info procs?pattern?返回过程列表
6info vars?pattern?返回变量列表

代码示例如下:
proc safeIncr {val {amt 1 }} {
upvar $val v
if {[info exists v]} {incr v $amt} else {
set v $amt}
} 
if {[info procs safeIncr] == "safeIncr"} {
safeIncr a
}
puts "after calling safeIncr with a non existent variable:$a"

set a 100
safeIncr a
puts "after calling safeIncr with a non existent variable of 100:$a"

safeIncr b -3
puts "after caliing safeIncr with a non existent variable by -3:$b"

set b 100
safeIncr b -3
puts "after calling safeIncr with a variable whose value is 100 by -3:$b"

puts "\n these variables have been defined:[lsort [info vars]]"
puts "\n these globals have been defined:[lsort [info globals]]"
运行结果如下:


2.检查本地过程是否存在

#检查本地过程是否存在
set exist [info procs localproc]
if {$exist == ""} {
puts "\nlocalproc does not exist at point 1"
}
proc localproc {} {
global argv;
set loc1 1
set loc2 2
puts "\n Local variables accessible in the proc are:[lsort [info locals]]"
puts "\n Variables are accessible from the proc are:[lsort [info vars]]"
puts "\n global variables visible from the proc are:[lsort [info globals]]"
}

set exist [info procs localproc]
if {$exist !=""} {
puts "localproc does exist at point 2"
}
localproc;
运行结果:


3.关于当前解释器状态信息命令列表

序号命令描述
1info cmdcount返回解释器已经运行的命令数量
2info level?number?返回相应栈级别的过程的名字和数量
3info patchlevel返回全局变量解释器补丁版本和修订号tcl_patchlevel的值
4info tclversion返回全局变量解释器版本tcl_version
5info script返回当前脚本的名字
6pid返回当前解释器进程id
示例:

puts "this is how many commands have been executed:\n[info cmdcount]"

puts "\nthis interpreter is revision level:[info tclversion]"
puts "this interpreter is at patch level:[info patchlevel]"

puts "\nthe temporary script executing now is called:[info script]"
puts "the pid for this program is [pid]"
运行结果;


4.过程信息

关于过程信息的命令主要有3个

  a. info args procname 返回一个过程的参数列表

  b info body procname 返回一个过程的内容

  c info default procname arg varName 如果一个过程的某个参数设置了缺省值则返回1,否则返回0

示例如下;

proc demo {arguement1 {default "defaultvalue"}} {
puts "this is a demo proc.it is being called with $arguement1 and $default"
}

puts "the args for demo are:[info args demo]\n"
puts "the body for demo is:[info body demo]\n"

set arglist [info args demo]

foreach arg $arglist {
if {[info default demo $arg defaultval]} {
puts "$arg has a default value of $defaultval"} else {
puts "$arg has no default"
}
}
运行结果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值