Libraries

By default, the unknown command tries to find procedure definitions in a library. A library is simply a file that contain procedure definitions. Libraries can be explicitly read using the source command. However, it is possible to prepare an index file which describes the library contents in such a way that Tcl knows which library to load based on the command name. Once a library is loaded, the unknown command calls the new procedure just defined. After the procedure completes, unknown completes and it appears as if the procedure had been defined all along.

As an example, one of Tcl’s default libraries defines the parray procedure. parray prints out the contents of an array. It is a parameterized version of the code on page 51. The info command shows that parray is not defined before it is invoked, but it is defined afterwards.

tclsh> info command parray
tclsh> parray color
color(pavement) = black
color(snow)     = white
tclsh> info command parray
parray

You can add procedures to the libraries or create new libraries. See the Tcl reference material for more information on using libraries.

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.