Version 5 of How to change a running system

Updated 2006-07-31 08:38:19 by suchenwi

Richard Suchenwirth 2001-06-10 -- Never change a running system, some of them say. But a Tcl interpreter with its accessories is a changing system from startup (every newly defined proc changes the language, for instance), and it's easy to extend or modify it while it runs.

Stefan Haefele wrote in the comp.lang.tcl newsgroup: I am trying to programm a user interface, which allows to add dynamically modules to this interface. The modules are tcl source files with several functions. Now I have problems to call the functions from the added tcl file.

Is there any easy way to let my application know where to call those functions?


Easiest way (pedestrian):

Assume you have your modules in Tcl files like 'module1.tcl' which contain only procedure definitions (well, maybe some variable settings too): For each module you want, just call

        source my/module/dir/module1.tcl

Here procs with the same names can be defined in different modules. The last one you source wins.


Classic way (auto):

Have your modules in one place (or few), create a TclIndex there (a Tcl script telling which file to source in order to get what proc). For this method, you'd better not have equal-named procs in different files, otherwise it's hard to predict what is sourced last. At startup of your application, just say

        lappend auto_path my/module/path ...  

Modern way:

Look at the 'package' command. In your module file, write

        package provide mymodule1 1.5

Make sure you have a valid package index. This Tcl source file describes what actions to take if a package is required. In your application, demand

        package require mymodule1 1.5

and all procedures that your package provides are available.


For experimenting, an interactive console is always great. As a starting point, look at a minimal console.

Don Porter has provided a console for Unix.

This console is known to work on Linux, win9x, and MAC.


Something that might be desired is the ability for the user to specify new directories or packages to be added dynamically, rather than hard coded packages. This should be simple - I believe if you take a look at tkconsole, it is already set up to discover what packages are known to tcl and dynamically setting of a menu (only at startup time I think though - too bad it doesn't notice when new things are installed...)

schlenk The basic package require mechanism detects new packages in the auto_path. You just have to require a nonexisting packages or use some of the more elaborate, but better, constructs always recommended by dgp. This indexes all packages and you can see what's there with package names.


Arts and crafts of Tcl-Tk programming