TCL學習之info命令

IamSarah發表於2017-04-28

這篇文章主要講一下info指令的相關用法。

1.info命令列表

該命令使用的匹配式規則和string match 一致,並且如果不適用匹配式,返回所有的項。

序號 命令 描述
1 info commands ?pattern? 返回匹配的命令列表
2 info exists varName 變數存在返回1,否則返回0
3 info globals?pattern? 返回全域性變數列表
4 info locals? pattern? 返回區域性變數列表
5 info procs?pattern? 返回過程列表
6 info 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.關於當前直譯器狀態資訊命令列表

序號 命令 描述
1 info cmdcount 返回直譯器已經執行的命令數量
2 info level?number? 返回相應棧級別的過程的名字和數量
3 info patchlevel 返回全域性變數直譯器補丁版本和修訂號tcl_patchlevel的值
4 info tclversion 返回全域性變數直譯器版本tcl_version
5 info script 返回當前指令碼的名字
6 pid 返回當前直譯器程式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"
}
}
執行結果如下:



相關文章