Package tips and tricks

Arjen Markus (13 february 2009) Just another page containing some tips and tricks, this one is about packages.

Whether you get a package from the teapot repository or from the distribution directly or ..., normally all you need to do is:

   package require NAME

to load all parts of a package NAME and start using it.

Sometimes, however, you may want a bit of introspection:

  • Where is the package installed?
  • What DLLs or shared objects are involved?
  • What version have I loaded?

Here are some answers:

Where does Tcl look for a package?

Tcl uses the variable auto_path to look for a package. auto_path contains a list of directories to search. So if you want Tcl to look in other directories as well, simply lappend these directories to this variable.

Where is a particular package installed?

Here is a trick Andreas Kupries came up with to find out just where the files are located:

    package ifneeded NAME [package require NAME]

This command will return the exact command to get the package. It works regardless of the type of package: a Tcl-only package, a binary extension, or a Tcl module. You need to interpret the command yourself, but it contains the exact location.

(If you want to find where a particular version of the package is located, add the version information to the "package require" part)

What DLLs or shared objects are involved?

The following command returns information about the various DLLs/shared objects you currently have loaded into the interpreter:

    info loaded

What version have I loaded?

The simplest way to find out what version you have loaded is to repeat the "package require" command. As it has been loaded already, nothing in particular is done, but you do get the version number.

What other packages does a package use?

An approximate answer to the vexing question what other packages a particular package uses can be found this way (thanks to DGP):

  * Start with a Tcl/Tk shell
  * Record the packages that are present now: [[package names]] returns this information. Make a list of the packages already present:
    set packageList {}
    foreach p [package names] {
        catch {
            lappend packageList "$p [package present $p]"
        }
    }
  * Load the package in question (if successful, the required packages have been loaded)
  * Then make a new list of the present packages 
  * The difference between the two lists is your approximate answer

Caveats:

  * The package may require some of the packages that are already present
  * The package may load other packages when certain commands are used