Tuesday, June 12, 2012

TCL - Learning the existence of commands and variables ? - info


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 "*").
info commands ?pattern?
Returns a list of the commands, both internal commands and procedures, whose names match pattern.
info exists varName
Returns 1 if varName exists as a variable (or an array element) in the current context, otherwise returns 0.
info functions ?pattern?
Returns a list of the mathematical functions available via the expr command that match pattern.
info globals ?pattern?
Returns a list of the global variables that match pattern.
info locals ?pattern?
Returns a list of the local variables that match pattern.
info procs ?pattern?
Returns a list of the Tcl procedures that match pattern.
info vars ?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

No comments:

Post a Comment

Popular Posts