NaviServer Tcl Libraries - NaviServer Tcl Libraries
TABLE OF CONTENTS
DESCRIPTION
What Are Tcl Libraries?
When to Use Tcl Libraries
Tcl Libraries
Tcl Script Order of Evaluation
Tcl-only Modules
Configuration for Tcl-only Modules
Example of Tcl Initialization with Tcl-only Modules
Configuration for Tcl Libraries
How to Build and Debug Tcl Scripts
A Tcl library is simply a directory containing Tcl scripts that are sourced at startup by a virtual server. You can create private libraries for individual virtual servers and public libraries that affect all or some of an installation's virtual servers.
Each Tcl file in a library often contains one or more calls to ns_register_proc, ns_schedule_proc, or ns_register_filter to bind a script to a specific URL or URL hierarchy, plus the Tcl scripts that will handle the URL(s). This example shows the ns_register_proc function being used to bind the Tcl procedure "hello" to handle a GET request for /example/hello, plus the "hello" procedure itself:
ns_register_proc GET /example/hello hello
proc hello {conn context} {
ns_return $conn 200 text/plain "Hello World"
}
|
Tcl libraries can also be created that contain no registration functions; they may just contain Tcl scripts that are called from ADPs.
When NaviServer processes a method/URL request, it checks to see if there is a Tcl script in the virtual server's private or shared library to handle the method and URL. A private Tcl script registered to handle a URL overrides a shared Tcl script registered to handle the same URL.
The alternative to embedding Tcl scripts in HTML pages using ADPs (see Chapter 2), is to store Tcl scripts in Tcl libraries. The situations listed below are well-suited to the Tcl libraries approach.
Tcl libraries are initialized from a Tcl directory specified for each server. The private Tcl directory is specified with the Library configuration parameter in the ns/server/servername/tcl section. It defaults to /modules/tcl under NaviSerer hom einstallation. You can specify a Tcl directory for each server.
The shared Tcl libray directory is specified by tcllibrary parameter in the ns/parameters section and it defaults to /tcl under NaviServer home installation.
Note that the directories you specify need not reside under the NaviServer installation directory. This allows you to keep user-defined scripts physically separate from the scripts supplied by NaviServer.
At server startup time, Tcl initialization is performed in the following steps for the server:
As described in the "Tcl Libraries" section, you can define a Tcl directory for each server. However, none of the subdirectories under the Tcl directories will be initialized unless you load a corresponding module. For example, if the ServerA server has a Tcl directory defined as /home/mydir/tcl/a, and the nsdb and perm modules are loaded, then the following directories will be initialized as server start-up:
/home/mydir/tcl/a
/home/mydir/tcl/a/nsdb
/home/mydir/tcl/a/perm
If you want another directory under /home/tcl/a that contains Tcl scripts to be initialized also, you must load a Tcl-only module for it into the server using the "Tcl" keyword.
To load a Tcl-only module, add the following line to your configuration file:
ns_section "ns/server/servername/modules" ns_param mytcl Tcl
Then, at server start-up, the /home/mydir/tcl/a/mytcl directory will be initialized too. You can load any number of Tcl-only modules into a virtual server to have the Tcl scripts in the corresponding directories initialized.
For Tcl-only modules, no C module file is loaded. Only the corresponding Tcl directories are initialized.
This example shows demonstrates the order in which Tcl scripts are initialized at startup time for a server. The Library parameter is not set, so the library for S1 defaults to /modules/tcl. A Tcl-only module called M1 is loaded for S1 as follows:
ns_section "ns/server/S1/modules" ns_param M1 Tcl
The library for server S1 (/modules/tcl) contains these files:
abc.tcl
The library for module M1 (/modules/tcl/M1) contains these files:
init.tcl
priv.tcl
script1.tcl
The Tcl files will be sourced in this order:
/module/tcl/abc.tcl
/module/tcl/M1/init.tcl
/module/tcl/M1/priv.tcl
/module/tcl/M1/script1.tcl
Configuration for Tcl libraries is handled in the ns/server/server-name/tcl section of the configuration file. The parameters in that section are described in detail on page 61 of the NaviServer Administrator's Guide. Some parameters to note are:
To configure Tcl-only modules, see page 25.
Follow these steps to build and debug your Tcl scripts: