info(3tcl)

โœจ NAME โœจ

info(3tcl) โ€” Tcl Built-In Commands

info โ€” literally all the tea about the state of your Tcl interpreter ๐Ÿ’…


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

info option ?arg arg ...?

๐Ÿ’– DESCRIPTION ๐Ÿ’–

Okay so bestie, info is the main character of introspection โ€” it hands you the deets about what's going on inside the Tcl interpreter, no cap. Here's the whole menu of stuff you can ask it for:

info args procname

Returns the names of the parameters to the procedure named procname. Like, the whole lineup of arguments, right there. โœจ

info body procname

Returns the body of the procedure named procname. The actual guts of it. ๐Ÿซถ

info class subcommand class ?arg ...?

Returns information about the class named class. Peep CLASS INTROSPECTION down below for the full lore.

info cmdcount

Returns the total number of commands evaluated in this interpreter. Every. Single. One. ๐Ÿงฎ

info cmdtype commandName

Returns the type of the command named commandName. The built-in types are:

  • alias โ€” commandName was created by interp alias. โš ๏ธ Heads up: in a safe interpreter an alias is only visible if both the alias and the target are visible.
  • coroutine โ€” commandName was created by coroutine.
  • ensemble โ€” commandName was created by namespace ensemble.
  • import โ€” commandName was created by namespace import.
  • native โ€” commandName was created by the Tcl_CreateObjCommand interface directly without further registration of the type of command.
  • object โ€” commandName is the public command that represents an instance of oo::object or one of its subclasses.
  • privateObject โ€” commandName is the private command, my by default, that represents an instance of oo::object or one of its subclasses.
  • proc โ€” commandName was created by proc.
  • interp โ€” commandName was created by interp create.
  • zlibStream โ€” commandName was created by zlib stream.

info commands ?pattern?

Returns the names of all commands visible in the current namespace. If pattern is given, it returns only those names that match according to string match. But fr fr, only the last component of pattern is a pattern โ€” the other components identify a namespace. See NAMESPACE RESOLUTION in the namespace(3tcl) documentation for the details. ๐ŸŒธ

info complete command

Returns 1 if command is a complete command, and 0 otherwise. Typically used in line-oriented input environments so users can type in commands that span multiple lines. Lowkey essential. ๐Ÿ’…

info constant varName

Returns 1 if varName is a constant variable (see const) and 0 otherwise.

info consts ?pattern?

Returns the list of constant variables (see const) in the current scope, or the list of constant variables matching pattern (if that is provided) in a manner similar to info vars.

info coroutine

Returns the name of the current coroutine, or the empty string if there is no current coroutine or the current coroutine has been deleted. No coroutine, no name. Periodt. ๐ŸŽ€

info default procname parameter varname

If the parameter parameter for the procedure named procname has a default value, this stores that value in varname and returns 1. Otherwise, it returns 0.

info errorstack ?interp?

Returns a description of the active command at each level for the last error in the current interpreter, or in the interpreter named interp if given. ๐Ÿ”ฅ

The description is a dictionary of tokens and parameters. Tokens are currently either CALL, UP, or INNER, but ngl other values may be introduced in the future. CALL indicates a command call, and its parameter is the corresponding info level 0. UP indicates a shift in variable frames generated by uplevel or similar, and applies to the previous CALL item โ€” its parameter is the level offset. INNER identifies the "inner context", which is the innermost atomic command or bytecode instruction that raised the error, along with its arguments when available. So while CALL and UP give you a trail of the call path, INNER serves up the details of the offending operation in the innermost procedure call, even down to sub-expression granularity. ๐Ÿ‘‘

This information is also present in the -errorstack entry of the options dictionary returned by 3-argument catch; info errorstack is just a convenient way of retrieving it for uncaught errors at top-level in an interactive interpreter.

info exists varName

Returns 1 if a variable named varName is visible and has been defined, and 0 otherwise.

info frame ?depth?

Returns the depth of the call to info frame itself. Otherwise, it returns a dictionary describing the active command at depth, which counts all commands visible to info level, plus commands that don't create a new level, such as eval, source, or uplevel. The frame depth is always greater than the current level. ๐Ÿงš

If depth is greater than 0 it is the frame at that depth. Otherwise it is the number of frames up from the current frame.

As with info level and error traces, for nested commands like foo [bar [x]], only x is seen by info frame invoked within x.

The dictionary may contain the following keys:

  • type โ€” Always present. Possible values are source, proc, eval, and precompiled.
    • source โ€” A script loaded via the source command.
    • proc โ€” The body of a procedure that could not be traced back to a line in a particular script.
    • eval โ€” The body of a script provided to eval or uplevel.
    • precompiled โ€” A precompiled script (loadable by the package tbcload), and no further information is available.
  • line โ€” The line number of the command inside its script. Not available for precompiled commands. When the type is source, the line number is relative to the beginning of the file, whereas for the last two types it is relative to the start of the script.
  • file โ€” For type source, provides the normalized path of the file that contains the command.
  • cmd โ€” The command before substitutions were performed.
  • proc โ€” For type proc, the name of the procedure containing the command.
  • lambda โ€” For a command in a script evaluated as the body of an unnamed routine via the apply command, the definition of that routine.
  • level โ€” For a frame that corresponds to a level, (to be determined).

When a command can be traced to its literal definition in some script, e.g. procedures nested in statically defined procedures, and literal eval scripts in files or statically defined procedures, its type is source and its location is the absolute line number in the script. Otherwise, its type is proc and its location is its line number within the body of the procedure.

In contrast, procedure definitions and eval within a dynamically evaluated environment count line numbers relative to the start of their script, even if they would be able to count relative to the start of the outer dynamic script. That type of number usually makes more sense.

A different way of describing this behaviour is that file-based locations are tracked as deeply as possible, and where this is not possible the lines are counted based on the smallest possible eval or procedure body, as that scope is usually easier to find than any dynamic outer scope.

The syntactic form {*} is handled like eval. I.e. if it is given a literal list argument the system tracks the line number within the list words as well, and otherwise all line numbers are counted relative to the start of each word (smallest scope)

info functions ?pattern?

If pattern is not given, returns a list of all the math functions currently defined. If pattern is given, returns only those names that match pattern according to string match. โœจ

info globals ?pattern?

If pattern is not given, returns a list of all the names of currently-defined global variables. Global variables are variables in the global namespace. If pattern is given, only those names matching pattern are returned. Matching is determined using the same rules as for string match.

info hostname

Returns the name of the current host. ๐Ÿ 

Bestie, listen: this name is not guaranteed to be the fully-qualified domain name of the host. Where machines have several different names โ€” as is common on systems with both TCP/IP (DNS) and NetBIOS-based networking installed โ€” it is the name that is suitable for TCP/IP networking that is returned.

info level ?level?

If number is not given, the level this routine was called from. Otherwise returns the complete command active at the given level. If number is greater than 0, it is the desired level. Otherwise, it is number levels up from the current level. A complete command is the words in the command, with all substitutions performed, meaning that it is a list. See uplevel for more information on levels.

info library

Returns the value of tcl_library, which is the name of the library directory in which the scripts distributed with Tcl scripts are stored. ๐Ÿ“š

  • info loaded ?interp? ?prefix? โ€” tells you the name of every file that got loaded into interp by the load command with prefix prefix. No prefix given? Then you get a list where each item is the name of the loaded file plus the prefix it was loaded for. For a statically-loaded package the name of the file is the empty string, and for interp the empty string means the current interpreter. ๐ŸŽ€
  • info locals ?pattern? โ€” hand it a pattern and it returns the name of each local variable that matches pattern according to string match. No pattern? Then it just returns the name of each local variable, periodt. Quick warning bestie: a variable defined with global, upvar or variable is not local, so don't come for me when it doesn't show up. ๐Ÿ’…
  • info nameofexecutable โ€” returns the absolute pathname of the program for the current interpreter. And if such a file can't be identified? You get an empty string back. ๐Ÿ˜ญ
  • info object subcommand object ?arg ...? โ€” returns information about the object named object. The subcommand is described down in OBJECT INTROSPECTION. โœจ
  • info patchlevel โ€” returns the value of the global variable tcl_patchLevel, where the exact version of the Tcl library was initially stored. ๐Ÿ‘‘
  • info procs ?pattern? โ€” returns the names of all visible procedures. Give it a pattern and you only get the names that match according to string match. Important: only the final component in pattern is actually treated as a pattern โ€” any qualifying components just select a namespace. Peep NAMESPACE RESOLUTION in the namespace(3tcl) docs for the whole tea. ๐Ÿซถ
  • info script ?filename? โ€” returns the pathname of the innermost script currently being evaluated, or the empty string if no pathname can be figured out. If filename is given, it sets the return value of any future calls to info script for the duration of the innermost active script. Lowkey so useful in virtual file system applications. ๐Ÿฆ‹
  • info sharedlibextension โ€” returns the extension this platform uses for names of shared libraries, e.g. .so under Solaris. If shared libraries aren't supported on this platform, you get the empty string. ๐Ÿ’€
  • info tclversion โ€” returns the value of the global variable tcl_version, where the major and minor version of the Tcl library live. ๐ŸŒธ
  • info vars ?pattern? โ€” no pattern? Then you get the names of all visible variables. Got a pattern? Then you only get the names matching according to string match. Only the last component of pattern is a pattern โ€” the other components identify a namespace. See NAMESPACE RESOLUTION in the namespace(3tcl) documentation. And when pattern is a qualified name, results come back fully qualified, no cap. Also, a variable that's been declared but not yet given a value will be included in the results. ๐Ÿงš

๐Ÿ”ฎ CLASS INTROSPECTION ๐Ÿ”ฎ

These are the subcommand values that info class supports, bestie โ€” get comfy:

  • info class call class method โ€” returns a description of the method implementations used to provide a stereotypical instance of class's implementation of method (stereotypical instances = objects instantiated by a class without any object-specific definitions added, iykyk). What you get is a list of lists of four elements, where each sublist has:
    • a word describing the general type of method implementation โ€” one of method for an ordinary method, filter for an applied filter, private for a private method, and unknown for a method invoked as part of unknown method handling;
    • a word giving the name of the particular method invoked (which is always the same as method for the method type, and unknown for the unknown type);
    • a word giving the fully qualified name of the class that defined the method;
    • a word describing the type of method implementation (see info class methodtype).
Heads up: there is no inspection of whether the method implementations actually use next to transfer control along the call chain, and the call chains that this command files do not actually contain private methods. Not me being dramatic, that's literally how it is. ๐Ÿ’€
  • info class constructor class โ€” returns a description of the definition of the constructor of class class. It's a two element list: the first element is the list of arguments to the constructor in a form suitable for passing to another call to proc or a method definition, and the second element is the body of the constructor. No constructor present? Empty list, bye. ๐ŸŽ€
  • info class definition class method โ€” returns a description of the definition of the method named method of class class. Two element list again: first element is the list of arguments to the method in a form suitable for passing to another call to proc or a method definition, second element is the body of the method. โœจ
  • info class definitionnamespace class ?kind? โ€” returns the definition namespace for kind definitions of the class class. Note that the definition namespace only affects the instances of class, not class itself. The kind can be either -class, to return the definition namespace used for oo::define, or -instance, to return the definition namespace used for oo::objdefine; the -class kind is the default (though this is only actually useful on classes that are subclasses of oo::class).

If class doesn't provide a definition namespace of the given kind, this command returns the empty string. In those circumstances, the oo::define and oo::objdefine commands look up which definition namespace to use via the class inheritance hierarchy. ๐ŸŒˆ

  • info class destructor class โ€” returns the body of the destructor of class class. If there's no destructor present, you get the empty string. ๐Ÿ™ˆ
  • info class filters class โ€” returns the list of filter methods set on the class. ๐Ÿ“
  • info class forward class method โ€” returns the argument list for the method forwarding called method that's set on the class called class. ๐Ÿ”ฅ
  • info class instances class ?pattern? โ€” returns a list of instances of class class. If that optional pattern argument is there, it constrains the list of returned instances to the ones that match it according to the rules of string match. ๐Ÿ’œ
  • info class methods class ?options...? โ€” returns a list of all public (i.e. exported) methods of the class called class. You can pass any of these options to control exactly which method names come back:
    • -all โ€” if the -all flag is given, and the -scope flag is not given, the list of methods will include those methods defined not just by the class, but also by the class's superclasses and mixins. ๐Ÿ‘‘
    • -private โ€” if the -private flag is given, and the -scope flag is not given, the list of methods will also include the non-exported methods of the class (and superclasses and mixins, if -all is also given). Note that this naming is an unfortunate clash with true private methods; this option name is retained for backward compatibility. ๐Ÿ˜ญ
    • -scope scope โ€” returns a list of all methods on class that have the given visibility scope. When this option is supplied, both the -all and -private options are ignored. The valid values for scope are:
      • public โ€” only methods with public scope (i.e., callable from anywhere by any instance of this class) are to be returned.
      • unexported โ€” only methods with unexported scope (i.e., only callable via my) are to be returned.
      • private โ€” only methods with private scope (i.e., only callable from within this class's methods) are to be returned.
  • info class methodtype class method โ€” returns a description of the type of implementation used for the method named method of class class. When the result is method, you can dig up more info with info class definition, and when the result is forward, you can dig up more info with info class forward. ๐Ÿฆ‹
  • info class mixins class โ€” returns a list of all classes that have been mixed into the class named class. ๐Ÿงš
  • info class properties class ?options...? โ€” returns a sorted list of properties defined on the class named class. The options define exactly which properties come back:
    • -all โ€” with this option, the properties from the superclasses and mixins of the class are also returned. โœจ
    • -readable โ€” this option (the default behavior) asks for the readable properties to be returned. Only readable or writable properties are returned, not both. ๐ŸŒธ
    • -writable โ€” this option asks for the writable properties to be returned. Only readable or writable properties are returned, not both. ๐Ÿ’…
  • info class subclasses class ?pattern? โ€” drops a list of the direct subclasses of class. If you toss in the optional pattern, it narrows the returned classes down to only the ones that match it, following the rules of string match.
  • info class superclasses class โ€” gives you the list of direct superclasses of class, listed in inheritance precedence order. no notes, just vibes ๐Ÿ’…
  • info class variables class ?-private? โ€” returns a list of every variable that's been declared for the class named class (aka the ones that are just automatically present in the class's methods, constructor and destructor ๐Ÿซถ). If you pass the -private option, then instead you get the private variables that were declared. โœจ

โœจ OBJECT INTROSPECTION โœจ

Here's the whole lineup of subcommand values that info object supports, bestie โ€” buckle up:

  • info object call object method โ€” returns a description of the method implementations that are used to provide object's implementation of method. It's a list of lists, four elements each, and every sublist has:
    • a word describing the general type of method implementation โ€” one of method for an ordinary method, filter for an applied filter, private for a private method, and unknown for a method that gets invoked as part of unknown method handling ๐Ÿ™ˆ
    • a word giving the name of the particular method invoked (which is always the same as method for the method type, and โ€œunknownโ€ for the unknown type)
    • a word giving what defined the method (either the fully qualified name of the class, or the literal string object if the method implementation lives on an instance)
    • a word describing the type of method implementation (peep info object methodtype for that one)
Heads up: there is no inspection of whether the method implementations actually use next to transfer control along the call chain, and the call chains that this command files do not actually contain private methods.
  • info object class object ?className? โ€” if className isn't given, this returns the class of the object object. If className is there, you get back a boolean value telling you whether the object is of that class. simple as that ๐Ÿ’–
  • info object creationid object โ€” returns the unique creation identifier for the object object. This creation identifier is unique to the object (within a Tcl interpreter) and cannot be controlled at object creation time or altered afterwards. periodt.
Implementation note: the creation identifier is used to generate unique identifiers associated with the object, especially for private variables. ๐Ÿงš
  • info object definition object method โ€” returns a description of the definition of the method named method of object object. The definition comes as a two element list: the first element is the list of arguments to the method, in a form suitable for passing to another call to proc or a method definition, and the second element is the body of the method. iconic.
  • info object filters object โ€” returns the list of filter methods set on the object. ๐ŸŒธ
  • info object forward object method โ€” returns the argument list for the method forwarding called method that is set on the object called object.
  • info object isa category object ?arg? โ€” tests whether an object belongs to a particular category, returning a boolean value that indicates whether the object argument meets the criteria for the category. Here are the supported categories:
    • info object isa class object โ€” returns whether object is a class (i.e. an instance of oo::class or one of its subclasses).
    • info object isa metaclass object โ€” returns whether object is a class that can manufacture classes (i.e. is oo::class or a subclass of it). it's giving class-factory energy ๐Ÿ‘‘
    • info object isa mixin object class โ€” returns whether class is directly mixed into object.
    • info object isa object object โ€” returns whether object really is an object. vibe check fr fr
    • info object isa typeof object class โ€” returns whether class is the type of object (i.e. whether object is an instance of class or one of its subclasses, whether direct or indirect).
  • info object methods object ?option...? โ€” returns a list of all public (i.e. exported) methods of the object called object. You can give any of these options, which control exactly which method names come back:
    • -all โ€” if the -all flag is given, and the -scope flag is not given, the list of methods will include those methods defined not just by the object, but also by the object's class and mixins, plus the superclasses of those classes. the whole family tree basically ๐ŸŒˆ
    • -private โ€” if the -private flag is given, and the -scope flag is not given, the list of methods will also include the non-exported methods of the object (and classes, if -all is also given).
Note that this naming is an unfortunate clash with true private methods; this option name is retained for backward compatibility. ๐Ÿ’€
  • -scope scope โ€” returns a list of all methods on object that have the given visibility scope. When this option is supplied, both the -all and -private options are ignored. The valid values for scope are:
    • public โ€” only methods with public scope (i.e., callable from anywhere) are to be returned.
    • unexported โ€” only methods with unexported scope (i.e., only callable via my) are to be returned.
    • private โ€” only methods with private scope (i.e., only callable from within this object's instance methods) are to be returned.
  • info object methodtype object method โ€” returns a description of the type of implementation used for the method named method of object object. When the result is method, you can dig up more with info object definition, and when the result is forward, you can dig up more with info object forward. slay ๐Ÿ”ฅ
  • info object mixins object โ€” returns a list of all classes that have been mixed into the object named object.
  • info object namespace object โ€” returns the name of the internal namespace of the object named object.
  • info object properties object ?options... โ€” returns a sorted list of properties defined on the object named object. The options define exactly which properties come back:
    • -all โ€” with this option, the properties from the class, superclasses and mixins of the object are also returned.
    • -readable โ€” this option (the default behavior) asks for the readable properties to be returned. Only readable or writable properties are returned, not both.
    • -writable โ€” this option asks for the writable properties to be returned. Only readable or writable properties are returned, not both.
  • info object variables object ?-private? โ€” returns a list of all variables that have been declared for the object named object (i.e. that are automatically present in the object's methods). If the -private option is given, this lists the private variables declared instead. ๐ŸŽ€
  • info object vars object ?pattern? โ€” returns a list of all variables in the private namespace of the object named object. If the optional pattern argument is given, it's a filter (in the syntax of a string match glob pattern) that constrains the list of variables returned.
Note that this is different from the list returned by info object variables; that one can include variables that are currently unset, whereas this one can include variables that are not automatically included by any of object's methods (or those of its class, superclasses or mixins). ๐Ÿ˜ญ

โœจ EXAMPLES โœจ

Okay bestie, this first one is so cute โ€” it prints out a whole procedure formatted so you can just save it straight into a Tcl script. Obsessed:

proc printProc {procName} {
    set result [list proc $procName]
    set formals {}
    foreach var [info args $procName] {
        if {[info default $procName $var def]} {
            lappend formals [list $var $def]
        } else {
            # Still need the list-quoting because variable
            # names may properly contain spaces.
            lappend formals [list $var]
        }
    }
    puts [lappend result $formals [info body $procName]]
}

๐Ÿฆ‹ EXAMPLES WITH OBJECTS ๐Ÿฆ‹

Every single object literally knows what its class is โ€” it's baked in. So pulling that info out is basically free, pure introspection vibes:

oo::class create c
c create o
puts [info object class o]
                     โ†’ prints "::c"
puts [info object class c]
                     โ†’ prints "::oo::class"

See how o is just a lil guy of class ::c, and c itself is an object of class ::oo::class? main character energy for real ๐Ÿ‘‘

Now the glow-up: you can use introspection to figure out which class actually implements a method, and grab how it's defined. This procedure is giving detective ๐Ÿ•ต๏ธโ™€๏ธ:

proc getDef {obj method} {
    foreach inf [info object call $obj $method] {
        lassign $inf calltype name locus methodtype

        # Assume no forwards or filters, and hence no $calltype
        # or $methodtype checks...

        if {$locus eq "object"} {
            return [info object definition $obj $name]
        } else {
            return [info class definition $locus $name]
        }
    }
    error "no definition for $method"
}

And here's the alternate route to the same definition โ€” this one manually walks the method list up the inheritance tree. Heads up though: this code assumes only single inheritance is happening, and that nobody's doing anything fancy with mixed-in classes. If that is happening, using info object call like above is by far the simplest way to do this, no cap:

proc getDef {obj method} {
    if {$method in [info object methods $obj]} {
        # Assume no forwards
        return [info object definition $obj $method]
    }

    set cls [info object class $obj]

    while {$method ni [info class methods $cls]} {
        # Assume the simple case
        set cls [lindex [info class superclass $cls] 0]
        if {$cls eq ""} {
            error "no definition for $method"
        }
    }

    # Assume no forwards
    return [info class definition $cls $method]
}

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

Go read these next, they're all besties with this page ๐Ÿ’–


๐Ÿ”‘ KEYWORDS ๐Ÿ”‘

The vibe check words for this page, fr fr:

command, information, interpreter, introspection, level, namespace, object, procedure, variable ๐ŸŒธ


And that's a wrap on the whole page, bestie! ๐ŸŽ€ This one's from Tcl 8.4 and it's filed under info(3tcl) โ€” so if you ever forget where you read it, you know exactly where to look. Slay, go introspect something ๐Ÿ’…โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from