How to change a running system

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 comp.lang.tcl: 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:

        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.


Finally, Tcl modules are packages designed to load very quickly - a module is a single file in one well-defined place (no search necessary).


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.


George Peter Staplin 2006.08.01 - Whim has an interactive console, and my primary Tcl code editor ProcMeUp has an "Attach" option that works with the "Sync" button to send updated procs to a running attached application (via send). It's much faster than restarting an app. I wish I had done this long ago.


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.


RS 2007-10-04: One new and very fascinating way is to change the system by adding C functions interactively, e.g. with Odyce:

 31% critcl::cproc sigmsg {int i} char* {return Tcl_SignalMsg(i);}
 32% sigmsg 2
 interrupt