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 variable error, since it checks to be
certain that the variable exists before incrementing it:
proc safeIncr {val {amount 1}} { upvar $val v if { [info exists v] } { incr v $amount } 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 matchpattern
. 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 } puts "After calling SafeIncr with a non existent variable: $a" set a 100 safeIncr a puts "After calling SafeIncr with a variable with a value of 100: $a" safeIncr b -3 puts "After calling 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 "\nThese variables have been defined: [lsort [info vars]]" puts "\nThese globals have been defined: [lsort [info globals]]" 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 "\nLocal variables accessible in this proc are: [lsort [info locals]]" puts "\nVariables accessible 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