Forum OpenACS Q&A: Re: New twist on age old 'package require' problems

Posted by Andrew Piskorski on
Does it matter that TclCurl needs to load a C module as well as Tcl code?

I also tried to create a genuine "nscurl" AOLserver module based on TclCurl, but since I couldn't get TclCurl to build at all that didn't get very far.

Note that TclCurl ships with a file doc/aolserver.txt which purports to explain how to use TclCurl with AOLserver 3.x, but it is cryptic and doesn't seem to make sense. Specifically, it mentions a file "modules\tcl\am\packages.init.tcl", but AFAICT no such file nor any simiar file exists in AOLserver 3.3+ad13.

Here are those TclCurl doc/aolserver.txt instructions:

There are a number of issues with namespaces in AOLserver 3.x, which I believe
are fixed in 4.0, which should be released within a few months. But in the
meantime this is what we've had to do for AOLserver 3.2 on Windows 2000.

Alex Khassin

1.  Under [ns_library shared] directory, create a directory called

2.  Register this directory as a Tcl module in nsd.tcl:
        ns_section "ns/server/${servername}/modules"
        ns_param   packages Tcl

3.  Place each package into a subdirectory of the same name as the
    package name (i.e. [ns_library shared]/packages/TclCurl)

4.  Copy S:\NaviSoft\Server\modules\tcl\am\packages.init.tcl to the
    [ns_library shared]/packages directory and rename to just init.tcl

5.  Under AOLserver 4.x (and hopefully in 3.5.x) add to the bottom
    of this file appropriate commands to register each package:
        _am_pregister shared <packageName>

6.  In your code, when you need to use a particular package, instead
    of 'package require <packageName>', execute 'am_pinit <packageName>'

7.  This will use the existing package registration under AOLserver
    4.x and, under AOLserver 3.2, it will first register the package
    in this interpreter and then use it.

8.  This is necessary because in AOLserver 3.2, namespaces aren't
    properly imported into child interpreters.

    Currently is set up like this for TclCurl and it works.

    Example usage:

        am_pinit TclCurl
        curl::transfer -url -file d:/test.htm

FYI, the code for am_pinit and _am_pregister procs:

        proc am_pinit {package} {
        # AOLserver 3.2 package/namespace-handling is broken
        # (namespace/packages don't get imported into child interpreters)
        # so use this workaround proc instead of 'package require' to
        # load the package into the current interpreter
        # (this is obviously slower than copying from master interpeter)
        # Package names are case-sensitive!
        # Returns the version of the loaded package
            set library shared
            if {[lsearch -exact [package names] $package] == -1} {
                ns_log Notice "packages: registering $library/$package"
                _am_pregister $library $package
            package require $package
        proc _am_pregister {library package} {
        # Registers the package. library is 'shared' or 'private'
            set dir [ns_library $library]/packages/$package
            source $dir/pkgIndex.tcl