:TITLE: Building Libraries of procs - unknown, info library ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl31.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# ::CMD:: if {([info exists tcl_platform])} { switch $tcl_platform(platform) { unix { set Tutor(lsn.codeMod) { } } windows { set Tutor(lsn.codeMod) { {global env} {regsub -all {\\} $env(COMSPEC) {/} cspec} {regsub {cat} $line " $cspec /C type" line} {if {[glob -nocomplain C:/temp] != ""} { regsub "/tmp" $line {C:/temp} line } } {if {[glob -nocomplain C:/windows/temp] != ""} { regsub "/tmp" $line {C:/windows/temp} line } } {if {[glob -nocomplain C:/winnt/temp] != ""} { regsub "/tmp" $line {C:/winnt/temp} line } } } } mac { set Tutor(lsn.codeMod) { } } default { puts "I don't recognize the platform: $tcl_platform(platform)" puts "Can't set platform specific parameters" } } } :LESSON_TEXT_START_LEVEL 0: Tcl provides a facility for automatically loading procs when they are first accessed, instead of sourcing them when the application starts. This can shorten an application's startup time, and even shrink the memory usage if not all procs are accessed during the application's use.

The mechanism for this process is that files are placed in a directory included in the auto_path list of directories. Each of these directories needs an index, which is created by the auto_mkindex command. When an unrecognized proc is called, it generates a call to the unknown proc, which searches through the index files in the list of directories until it finds a match to the unknown proc. When a match is found, the unknown proc automatically loads that file, and evaluates the proc, returning control to the original script as though the proc had been there all along.

auto_mkindex libdir file1 ... filen
Creates an index file (tclIndex) in libdir from the source code modules listed as file1 - filen. Each proc in the files will be listed in the index file, with a reference to the source code module that contains it.

The file descriptors may be any number of strings using the same format as arguments to the glob command.

This proc is defined in init.tcl which is loaded from the default library directory when the Tcl shell is initialized.

info library
Returns the name of the library directory in which the standard Tcl scripts are stored.

Tcl procs that are in files in the Tcl library directory are loaded automatically when they are executed.

Tcl expects to find init.tcl in the default library location, defined when the Tcl interpreter is built. This path can be changed by setting the environment variable TCL_LIBRARY.

unknown args
This proc is called by the interpreter when it encounters a command that can be parsed, but the command name is not in the tables. Unknown attempts the following steps to execute the command
:TEXT_END: :LESSON_TEXT_START_LEVEL 1: When a Tcl script starts executing, you can immediately source all the files that may need to be accessed during the execution of the script, or you can wait until procs are actually called and source the files at that time.

Tcl provides a facility for automatically loading procs when they are first accessed, instead of sourcing them when the application starts. Delaying the source call can shorten an application's startup time, and even shrink the memory usage if not all procs are accessed during the application's use.

The procedure to follow to enable Tcl to automatically load files is this:

  1. Place the source files in a single directory
  2. Create an index file (tclIndex) in that directory using auto_mkindex
  3. Add this directory path to the list of directories to be searched by lappending this path to the auto_path global variable in your application script.

When an unrecognized proc is called, Tcl generates a call to the unknown proc. Unknown searches through the index files in the directory list auto_path until it finds a match to the unknown proc. When a match is found, unknown automatically loads that file, and evaluates the proc, returning control to the original script as though the proc had been there all along.

auto_mkindex libdir file1 ... filen
Creates an index file (tclIndex) in libdir from the source code modules listed as file1 - filen. Each proc in the files will be listed in the index file, with a reference to the source code module that contains it.

The file descriptors may be any number of strings using the same format as arguments to the glob command.

This proc is defined in init.tcl which is loaded from the default library directory when the Tcl shell is initialized.

info library
Returns the name of the library directory in which the standard Tcl scripts are stored.

Tcl procs that are in files in the Tcl library directory are loaded automatically when they are executed.

Tcl expects to find init.tcl in the default library location, defined when the Tcl interpreter is built. This path can be changed by setting the environment variable TCL_LIBRARY.

unknown args
This proc is called by the interpreter when it encounters a command that can be parsed, but the command name is not in the tables. Unknown attempts the following steps to execute the command
:TEXT_END: :LESSON_TEXT_START_LEVEL 2: When a Tcl script starts executing, you can immediately source all the files that contain procs that may be accessed during the execution of the script, or you can wait until procs are actually called and source the files at that time.

sourcing all the files when you first start an application can make the application start up very slowly.

Checking that each proc exists before you call it, and sourcing the file at that time makes a lot of extra code, and makes an application run slowly.

Tcl provides a facility for automatically loading procs when they are first accessed, instead of sourcing them when the application starts. This lets you shorten your applications startup time without adding any extra code to check that procs have been defined. sourcing modules only when necessary may also reduce a tasks memory requirements since not all procs may be needed each time it runs.

The procedure to follow to enable Tcl to automatically load files is this:

  1. Copy all the source files in a single directory.
  2. Create an index file (tclIndex) in that directory using auto_mkindex.
  3. Add this directory path to the list of directories to be searched by lappending this path to the auto_path global variable in your application script.

When the Tcl interpreter encounters a proc that hasn't been defined, it calls the unknown proc. Unknown searches through the index files in the directory list stored in the global variable auto_path until it finds a match to the unknown proc. When a match is found, unknown automatically loads that file, and evaluates the proc, returning control to the original script as though the proc had been there all along.

Run the example, and examine the code and output while you read the description here.

The first portion of the example just creates a temporary file, the same way that it was created in the previous lesson.

After this, /tmp is appended to the list of directories to search when an undefined proc is encountered. Then that list is printed. It will probably have two entries on your machine. The first two will be the library paths where the Tcl and Tk interpreters expect to find initialization files. After that will be /tmp, the directory we just added.

After this, the compiler default library directory is printed . This directory will be one of the two first libraries from the previous output.

The next line makes the index file. In this case, it defines only one file to be placed in the index. The second argument could be *.tcl, in which case any files ending in .tcl would be added to the index.

After this, the code confirms that testproc has not been defined, and then executes testproc anyhow. The output shows that testproc wasn't defined originally, but executed OK. After testproc was called, the code confirms that it still exists.

Finally, the temporary files are cleaned up.

auto_mkindex libdir file1 ... filen
Creates an index file (tclIndex) in libdir from the source code modules listed as file1 - filen. Each proc in the files will be listed in the index file, with a reference to the source code module that contains it.

The file descriptors may be any number of strings using the same format as arguments to the glob command.

This proc is defined in init.tcl which is loaded from the default library directory when the Tcl shell is initialized.

info library
Returns the name of the library directory in which the standard Tcl scripts are stored.

Tcl procs that are in files in the Tcl library directory are loaded automatically when they are executed.

Tcl expects to find init.tcl in the default library location, defined when the Tcl interpreter is built. This path can be changed by setting the environment variable TCL_LIBRARY.

unknown args
This proc is called by the interpreter when it encounters a command that can be parsed, but the command name is not in the tables. Unknown attempts the following steps to execute the command
:TEXT_END: :CODE_START: ;# Set up a temporary file with a test proc. set filename "/tmp/TT_[pid]" set outfile [open "$filename" "w"]; puts $outfile {set scr [info script]} puts $outfile "proc testproc {} {" puts $outfile "global scr;" puts $outfile "puts \"testproc source file: \$scr.\"" puts $outfile "}" close $outfile; puts "The directories in the auto path are: $auto_path\n" puts "The default library is: [info library]\n"; auto_mkindex "/tmp" [file tail $filename] # With Tcl8.3, this must come after auto_mkindex. lappend auto_path "/tmp" if { [info procs testproc] == ""} { puts "testproc does not exist\n" } testproc if { [info procs testproc] != ""} { puts "\ntestproc does exist now" } file delete /tmp/tclIndex file delete $filename :TEXT_END: