TCL - AutoLoad (TclLibPath)


If you're building a library of tcl scripts, you should consider using utilizing one of Tcl's auto-loading mechanisms.

The auto-loading mechanism will scan an index file (tclIndex, a dictionary of procedure) when a call to a procedure is made in order to find it.

No source command is then required.

To initiate this mechanism, you must:

  • create a tclIndex file
  • set the environment variable TcLLibPath with the value of the tclIndex directory

Then when you


Creation of the tclIndex file

Within each directory in the auto-load path there must be a file tclIndex which contains references to all the procedures defined in all the files.

An example is given below where the procedure myProc is present in the file MyFile localized in the TCLLIBPATH directory.

# Tcl autoload index file, version 2.0
# This file is generated by the "auto_mkindex" command
# and sourced to set up indexing information for one or
# more commands.  Typically each line is a command that
# sets an element in the auto_index array, where the
# element name is the name of a command and the value is
# a script that loads the command.

set auto_index(myProc) [list source [file join $dir MyFile.tcl]]
set auto_index(myProc2) [list source [file join $dir MyFile2.tcl]]
set auto_index(myProc3) [list source [file join $dir MyFile3.tcl]]

If you put a bunch of library scripts in a directory (or set of relative directories), you can use the command auto_mkindex command to generate automatically the tclIndex file.

auto_mkindex . *.tcl *.itb *.ith

where the point stand for the current directory.

Set up of the TCLLIBPATH Auto-load path environment variable

An auto-load path is a list of one or more directories.

The auto-load path is given by the global variable $auto_path if it exists.

/> puts $auto_path
resource:/tcl/lang/library {C:\gerardnico\OMB}

If there is no $auto_path variable, then the TCLLIBPATH environment variable is used, if it exists. Otherwise the auto-load path consists of just the Tcl library directory.

The seting of an environement variable depend of your OS:


Auto load cache

Auto_load only reads the index information once and saves it in the array auto_index; future calls to auto_load check for the procedure in the array rather than re-reading the index files.

The cached index information may be deleted with the command auto_reset. This will force the next auto_load command to reload the index database from disk.

Auto Load Command

auto_load cmd

This command attempts to load the definition for a Tcl command named cmd.

Documentation / Reference

Powered by ComboStrap