: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.
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.
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.
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:
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.
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.
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.
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:
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.
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.
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.