Forum OpenACS Q&A: package require

Collapse
Posted by Peter Harper on
I'm trying to use a tcl "package require" command within AolServer. After trawling through the AolServer mailing list, I've come up with the following hack which needs executing before doing the "package require":
  global tcl_version tcl_pkgPath tcl_library 
         tcl_platform auto_path
  set tcl_pkgPath            /usr/lib
  set tcl_library            [file join $tcl_pkgPath 
                              tcl${tcl_version}]
  set tcl_platform(platform) "unix"
  set auto_path              [list /usr/lib/tcl8.3 /usr/lib]
  source [file join $tcl_library init.tcl]
  source [file join $tcl_library package.tcl]
Its not only ugly, but also includes hardcoded stuff, which just won't do. Has anyone done this before, and maybe has a more elegant solution, or alternatively could think of a way to populate the tcl_pkgPath, auto_path and tcl_platform variables?

Cheers,

Collapse
Posted by Dave Bauer on
One answer is to not use package require at all and load the package Tcl files in your private Tcl library, commenting out the package commands. This will load the package in every Tcl interpreter that AOLserver loads which may not be exactly what you want.
Collapse
Posted by Peter Harper on
Thanks for that Dave. I'm currently in the process of integrating Tilmann's tclwebtest toolkit with acs-automated-testing. Tilmanns code performs a "package require", and currently pollutes the namespace (soon to be fixed), so I'd like to source within a single thread, only when necessary. If you're interested, see this thread for discussion of this integration work

Collapse
Posted by Dave Bauer on
Peter. When I use your hack, the init.tcl file causes a error when ns_sourceproc tries to do "global errorCode errorInfo" with a variable "errorCode" already exists.

You are loading the init.tcl and package.tcl from the tcl 8.3 distribution?

Collapse
Posted by Dave Bauer on
I have tracked down the offending code. The init.tcl from /usr/lib/tcl8.3 has:
<pre>
set errorCode ""
set errorInfo ""
</pre>
at line 124 or so.

If I comment that out, everything works fine. With is uncommented, when ns_sourceproc tries to do
<pre>
global errorCode errorInfo
</pre> i get:
<pre>
variable "errorCode" already exists
</pre>

This seems like strange behavior. I can't replicate it with any other variable in tcl.

Collapse
Posted by David Walker on
set myvar ""        
global myvar

will fail

global myvar        
set myvar ""        

will succeed.

Depending on your desires you may wish to change "global errorCode errorInfo" to
if {[info exists errorCode]} {    
	unset errorCode    
}    
if {[info exists errorInfo]} {    
	unset errorInfo    
}    
global errorCode errorInfo   
Collapse
Posted by Dave Bauer on
Darn,

I do now want to have to hack aolserver or tcl8.3 code to make this work .

I am confused as to why this sourcing of init.tcl and package.tcl are necessary in aolserver. I would hope those files, as part of the basic tcl distribution, would make it into all the interpreters.