Andrew Falanga wrote:
> Hello,
>
> I'm trying to debug why this the following is happening. I have a
> library in which I try to keep from performing a package require to many
> times. Mostly, this is to avoid errors when troubleshooting in the
> tclsh.
Not sure why you want to avoid package requires, it will only load/source it
the first time.
> My code looks like
>
>
> if { [lsearch [package names] "*cmdline*"] < 0 } {
> Log "cmdline is not present in package list"
> } else {
> Log "package cmdline is present in package list"
> }
>
> if { ![string match "*/path/to/tcllib/*" $auto_path] } {
> lappend auto_path /path/to/tcllib/
> }
>
> if { [lsearch [package names] "*cmdline*"] < 0 } {
> Log "cmdline is not present in package list"
> } else {
> Log "package cmdline is present in package list"
> }
>
> if { ![string match "*tclxml3.0*" $auto_path] } {
> lappend auto_path /path/to/tclxml3.0
> }
>
> if { [lsearch [package names] "*xml*"] < 0 } {
> package require xml
> }
>
> if { [lsearch [package names] "*cmdline*"] < 0 } {
> Log "cmdline is not present in package list"
> } else {
> Log "package cmdline is present in package list"
> }
>
> if { [lsearch [package names] "*cmdline*"] < 0 } {
> package require cmdline
> }
>
> The conditionals that do nothing but produce output are for debugging
> purposes only and produce the following output:
>
> cmdline is not present in package list
> cmdline is not present in package list
> package cmdline is present in package list
>
> Why? By the time of that third output, the package require cmdline
> hasn't been executed yet so why is it present?
[package names] returns the list of *available* packages, not the list of
loaded packages!
Doing a [package require] on a package with is not in the [package names]
list causes the auto_path to be walked to rebuild the list of available
packages. So when you do the package require on xml, you rebuild the list
of available packages -- now cmdline is there.
> Also, although my
> conditional is true, the procedures that use procs in the cmdline
> library, fail because the procedures haven't been included to the
> interpreter.
Because you never load/source it via the package require.
> Why does that first call to package require seem to "include" virtually
> all possible packages in the auto_path variable?
See above.
BTW, doing:
lappend auto_path /path/to/tcllib/ /path/to/tclxml3.0
package require xml
package require cmdline
Should always work just fine. It will not reload the package multiple times
or get confused if the path to it is in the auto_parh more than one time.
In other words, you are jumping through hoops to avoid a problem that does
not exists.
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
Received on Sun Apr 30 02:57:46 2006