Locating Packages: The auto_path Variable

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 12.  Script Libraries and Packages


Locating Packages: The auto_path Variable

The package facility assumes that Tcl libraries are kept in well-known directories. The list of well-known directories is kept in the auto_path Tcl variable. This is initialized by tclsh and wish to include the Tcl script library directory, the Tk script library directory (for wish), and the parent directory of the Tcl script library directory. For example, on my Macintosh auto_path is a list of these three directories:

 Disk:System Folder:Extensions:Tool Command Language:tcl8.2 Disk:System Folder:Extensions:Tool Command Language Disk:System Folder:Extensions:Tool Command Language:tk8.2 

On my Windows 95 machine the auto_path lists these directories:

 c:\Program Files\Tcl\lib\Tcl8.2 c:\Program Files\Tcl\lib c:\Program Files\Tcl\lib\Tk8.2 

On my UNIX workstation the auto_path lists these directories:

 /usr/local/tcl/lib/tcl8.2 /usr/local/tcl/lib /usr/local/tcl/lib/tk8.2 

The package facility searches these directories and their subdirectories for packages. The easiest way to manage your own packages is to create a directory at the same level as the Tcl library:

 /usr/local/tcl/lib/welchbook 

Packages in this location, for example, will be found automatically because the auto_path list includes /usr/local/tcl/lib. You can also add directories to the auto_path explicitly:

 lappend auto_path directory 

One trick I often use is to put the directory containing the main script into the auto_path. The following command sets this up:

 lappend auto_path [file dirname [info script]] 

If your code is split into bin and lib directories, then scripts in the bin directory can add the adjacent lib directory to their auto_path with this command:

 lappend auto_path \     [file join [file dirname [info script]] ../lib] 

       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net