, a built-in command, provides information about the state of a Tcl interpreter.Synopsis edit
- info option ?arg arg ...?
- info args procname
- info body procname
- info class subcommand class …
- info cmdcount
- info commands ?pattern?
- info complete script
- info coroutine
- info default procname arg varname
- info errorstack
- info exists varName
- info frame ?number?
- info functions pattern
- info globals ?pattern?
- info hostname
- info level ?number?
- info library
- info loaded ?interp?
- info locals ?pattern?
- info nameofexecutable
- info object subcommand object …
- info patchlevel
- info procs ?pattern?
- info script ?filename?
- info sharedlibextension
- info tclversion
- info vars ?pattern?
Documentation edit
Description edit
With info, you can find out about your Tcl environment. For exampleproc ls {{ns ::} {all no}} {
puts [list namespace $ns]
foreach child [namespace children $ns] {
if {{all} eq $all} {
list $child all
} {
puts [list namespace $child]
}
}
set pat [set ns]::*
foreach proc [info procs $pat] {
puts [list proc $proc]
}
foreach var [info vars $pat] {
if {[array exists $var]} {
puts [list array $var]
} {
puts [list variable $var]
}
}
}now what would be a good way of extending that to list the significant symbols in interps?See tkinspectIn How can I tell where a proc was called
, comp.lang.tcl, 2001-05-30, in response to a question about finding a way to tell who called a particular proc, Bruce Hartweg wrote (modified):# quick tester:
proc who_called {} {
puts [list {I was called from} [
lindex [info level -1] 0] {but first call was to} [
lindex [info level 1] 0]]
}
proc a {} who_called
proc b {} who_called
proc c {} who_called
proc d {} a
proc e n {$n}
# endOutput:% a
{I was called from} a {but first call was to} a
% b
{I was called from} b {but first call was to} b
% c
{I was called from} c {but first call was to} c
% e a
{I was called from} a {but first call was to} e
% e b
{I was called from} b {but first call was to} e
% e c
{I was called from} c {but first call was to} e
% e d
{I was called from} a {but first call was to} eWill Taylor writes:I find a backtrace useful -- putting these two stmts in the proc in question:
set backtrace {}
getBackTrace backtrace
puts stderr [list [info level 0] [join $backtrace { => }]]Here is the backtrace proc:proc getBackTrace backTraceRef {
upvar $backTraceRef backTrace
set startLevel [expr {[info level] - 2}]
for {set level 1} {$level <= $startLevel} {incr level} {
lappend backTrace [lindex [info level $level] 0]
}
}The Bag of algorithms makes a couple of mentions of backtracing, and even has other uses of info ... .AM: A common idiom that I use:
set scriptdir [file dirname [info script]]so that I know where my other scripts or data files that need to be sourced/read are located.Another use: detect the difference between running the script on its own or as a package:
if {[file tail [info script]] eq [file tail $::argv0]} {
# The script is running on its own!
}There is one particular caveat: the command will return an empty string when it is called in an event handler - then there is no script being "sourced" anymore! So, save the result when you need it later on (that is: later on in the process, not later in the source file).
