Version 1 of How to change a running system

Updated 2001-10-10 23:38:18

Richard Suchenwirth -- Never change a running system, some of them say. But a Tcl interpreter with its accessories is a changing system from startup, 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 on this wiki.

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

Arts and crafts of Tcl-Tk programming