Tcl provides a number of commands for introspection - commands that tell what is going on in your program, what the implementation is of your procedures, which variables have been set and so on.
The
info command allows a Tcl program to obtain information from the Tcl interpreter. The next three lessons cover aspects of the info command. (Other commands allowing introspection involve: traces, namespaces, commands scheduled for later execution via the after command and so on.)This lesson covers the
info subcommands that return information about which procs, variables, or commands are currently in existence in this instance of the interpreter. By using these subcommands you can determine if a variable or proc exists before you try to access it.The code below shows how to use the
info exists command to make an incr that will never return a no such variableerror, since it checks to be certain that the variable exists before incrementing it:
proc safeIncr {val {amount 1}} {
upvar $val v
ists v] } {
incr v $
if { [info e
xamount
} else {
set v $amount
}
}
Info commands that return lists of visible commands and variables.
Almost all these commands take a pattern that follow the
string match rules. If pattern is not provided, a list of all items is returned (as if the pattern was "*").infocommands?pattern?- Returns a list of the commands, both internal commands and procedures, whose names match
pattern. infoexistsvarName- Returns 1 if
varNameexists as a variable (or an array element) in the current context, otherwise returns 0. infofunctions?pattern?- Returns a list of the mathematical functions available via the
exprcommand that matchpattern. infoglobals?pattern?- Returns a list of the global variables that match
pattern. infolocals?pattern?- Returns a list of the local variables that match
pattern. infoprocs?pattern?- Returns a list of the Tcl procedures that match
pattern. infovars?pattern?- Returns a list of the local and global variables that match
pattern.
Example
if {[info procs safeIncr] eq "safeIncr"} {
safeIncr a
}
SafeIncr with a non existent variable: $a"
set a 100
safeInc
puts "After callin
gr a
puts "After calling SafeIncr with a variable with a value of 100: $a"
3: $b"
set b
safeIncr b -3
puts "After calling safeIncr with a non existent variable by
- 100
safeIncr b -3
safeIncr with a variable whose value is 100 by -3: $b"
puts "\nThese vari
puts "After calling
ables have been defined: [lsort [info vars]]"
rt [info globals]]"
set exist [info procs localproc]
if {$exist =
puts "\nThese globals have been defined: [ls
o= ""} {
puts "\nlocalproc does not exist at point 1"
}
proc localproc {} {
global argv
set loc1 1
les accessible
set loc2 2
puts "\nLocal variables accessible in this proc are: [lsort [info locals]]"
puts "\nVaria
b from this proc are: [lsort [info vars]]"
puts "\nGlobal variables visible from this proc are: [lsort [info globals]]"
}
set exist [info procs localproc]
if {$exist != ""} {
puts "localproc does exist at point 2"
}
localproc

0 Comment:
Post a Comment